home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / emacs / site-lisp / dictionaries-common / ispell.el < prev   
Encoding:
Text File  |  2009-02-20  |  162.5 KB  |  4,109 lines

  1. ;;; ispell.el --- interface to International Ispell Versions 3.1 and 3.2
  2.  
  3. ;; Copyright (C) 1994, 1995, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
  4. ;;   2004, 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
  5.  
  6. ;; Author:           Ken Stevens <k.stevens@ieee.org>
  7. ;; Maintainer:       Ken Stevens <k.stevens@ieee.org>
  8. ;; Stevens Mod Date: Mon Jan  7 12:32:44 PST 2003
  9. ;; Stevens Revision: 3.6
  10. ;; Status          : Release with 3.1.12+ and 3.2.0+ ispell.
  11. ;; Bug Reports     : ispell-el-bugs@itcorp.com
  12. ;; Web Site        : http://kdstevens.com/~stevens/ispell-page.html
  13. ;; Keywords: unix wp
  14.  
  15. ;; This file is part of GNU Emacs.
  16.  
  17. ;; GNU Emacs is free software: you can redistribute it and/or modify
  18. ;; it under the terms of the GNU General Public License as published by
  19. ;; the Free Software Foundation, either version 3 of the License, or
  20. ;; (at your option) any later version.
  21.  
  22. ;; GNU Emacs is distributed in the hope that it will be useful,
  23. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  25. ;; GNU General Public License for more details.
  26.  
  27. ;; You should have received a copy of the GNU General Public License
  28. ;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
  29.  
  30. ;; --------------------------------------------------------------------
  31. ;; This file has been modified by Rafael Laboissiere <rafael@debian.org>
  32. ;; and Agustin Martin <agmartin@debian.org> for the Debian GNU/Linux
  33. ;; system to meet the requirements of the Debian Spelling Policy.
  34. ;;
  35. ;; Amongst other minor changes, ispell-dictionary-alist is set to
  36. ;; debian-ispell-dictionary-alist if present, containing entries for the
  37. ;; really installed and registered dictionaries, and ispell-dictionary is
  38. ;; set to the debian debconf selected value if possible
  39. ;; --------------------------------------------------------------------
  40.  
  41. ;; Note: version numbers and time stamp are not updated
  42. ;;   when this file is edited for release with GNU Emacs.
  43.  
  44. ;;; Commentary:
  45.  
  46. ;; INSTRUCTIONS
  47.  
  48. ;;   This code contains a section of user-settable variables that you
  49. ;; should inspect prior to installation.  Look past the end of the history
  50. ;; list.  Set them up for your locale and the preferences of the majority
  51. ;; of the users.  Otherwise the users may need to set a number of variables
  52. ;; themselves.
  53. ;;   You particularly may want to change the default dictionary for your
  54. ;; country and language.
  55. ;;   Most dictionary changes should be made in this file so all users can
  56. ;; enjoy them.  Local or modified dictionaries are supported in your .emacs
  57. ;; file.  Use the variable `ispell-local-dictionary-alist' to specify
  58. ;; your own dictionaries.
  59.  
  60. ;;  Depending on the mail system you use, you may want to include these:
  61. ;;  (add-hook 'news-inews-hook 'ispell-message)
  62. ;;  (add-hook 'mail-send-hook  'ispell-message)
  63. ;;  (add-hook 'mh-before-send-letter-hook 'ispell-message)
  64.  
  65. ;;   Ispell has a TeX parser and a nroff parser (the default).
  66. ;; The parsing is controlled by the variable ispell-parser.  Currently
  67. ;; it is just a "toggle" between TeX and nroff, but if more parsers are
  68. ;; added it will be updated.  See the variable description for more info.
  69.  
  70.  
  71. ;; TABLE OF CONTENTS
  72.  
  73. ;;   ispell-word
  74. ;;   ispell-region
  75. ;;   ispell-buffer
  76. ;;   ispell-message
  77. ;;   ispell-comments-and-strings
  78. ;;   ispell-continue
  79. ;;   ispell-complete-word
  80. ;;   ispell-complete-word-interior-frag
  81. ;;   ispell-change-dictionary
  82. ;;   ispell-kill-ispell
  83. ;;   ispell-pdict-save
  84. ;;   ispell-skip-region-alist
  85.  
  86. ;; Commands in ispell-region:
  87. ;; Character replacement: Replace word with choice.  May query-replace.
  88. ;; ` ': Accept word this time.
  89. ;; `i': Accept word and insert into private dictionary.
  90. ;; `a': Accept word for this session.
  91. ;; `A': Accept word and place in buffer-local dictionary.
  92. ;; `r': Replace word with typed-in value.  Rechecked.
  93. ;; `R': Replace word with typed-in value. Query-replaced in buffer. Rechecked.
  94. ;; `?': Show these commands
  95. ;; `x': Exit spelling buffer.  Move cursor to original point.
  96. ;; `X': Exit spelling buffer.  Leaves cursor at the current point, and permits
  97. ;;      the check to be completed later.
  98. ;; `q': Quit spelling session (Kills ispell process).
  99. ;; `l': Look up typed-in replacement in alternate dictionary.  Wildcards okay.
  100. ;; `u': Like `i', but the word is lower-cased first.
  101. ;; `m': Place entered value in personal dictionary, then recheck current word.
  102. ;; `C-l': redraws screen
  103. ;; `C-r': recursive edit
  104. ;; `C-z': suspend Emacs or iconify frame
  105.  
  106. ;; Buffer-Local features:
  107. ;; There are a number of buffer-local features that can be used to customize
  108. ;;  ispell for the current buffer.  This includes language dictionaries,
  109. ;;  personal dictionaries, parsing, and local word spellings.  Each of these
  110. ;;  local customizations are done either through local variables, or by
  111. ;;  including the keyword and argument(s) at the end of the buffer (usually
  112. ;;  prefixed by the comment characters).  See the end of this file for
  113. ;;  examples.  The local keywords and variables are:
  114.  
  115. ;;  ispell-dictionary-keyword   language-dictionary
  116. ;;      uses local variable ispell-local-dictionary
  117. ;;  ispell-pdict-keyword        personal-dictionary
  118. ;;      uses local variable ispell-local-pdict
  119. ;;  ispell-parsing-keyword      mode-arg extended-char-arg
  120. ;;  ispell-words-keyword        any number of local word spellings
  121.  
  122. ;; Region skipping:
  123. ;;  Place new regular expression definitions of regions you prefer not to
  124. ;;  spell check in `ispell-skip-region-alist'.  Mode-dependent features can
  125. ;;  be added to latex by modifying `ispell-tex-skip-alists'.
  126. ;;  `ispell-message' contains some custom skipping code for e-mail messages.
  127.  
  128. ;; BUGS:
  129. ;;  Need a way to select between different character mappings without separate
  130. ;;    dictionary entries.
  131. ;;  Multi-byte characters if not defined by current dictionary may result in the
  132. ;;    evil "misalignment error" in some versions of MULE Emacs.
  133. ;;  On some versions of Emacs, growing the minibuffer fails.
  134. ;;    see `ispell-help-in-bufferp'.
  135. ;;  Recursive edits (?C-r or ?R) inside a keyboard text replacement check (?r)
  136. ;;    can cause misalignment errors.
  137.  
  138. ;; HISTORY
  139.  
  140. ;; Modifications made in latest versions:
  141.  
  142. ;; Revision 3.6 2003/01/07 12:32:44    kss
  143. ;; Removed extra -d LIB in dictionary defs. (Pavel Janik)
  144. ;; Filtered process calls with duplicate dictionary entries.
  145. ;; Fixed bug where message-text-end is inside a mime skipped region.
  146. ;; Minor fixes to get ispell menus right in XEmacs
  147. ;; Fixed skip regexp so it doesn't match stuff like `/.\w'.
  148. ;; Detecting dictionary change not working.  Fixed.  kss
  149. ;; function `ispell-change-dictionary' now only completes valid dicts.
  150.  
  151. ;; Revision 3.5 2001/7/11 18:43:57    kss
  152. ;; Added fix for aspell to work in XEmacs (ispell-check-version).
  153. ;; Added Portuguese dictionary definition.
  154. ;; New feature: MIME mail message support, Fcc support.
  155. ;; Bug fix: retain comment syntax on lines with region skipping. (TeX $ bug...)
  156. ;; Improved allocation for graphic mode lines.  (Miles Bader)
  157. ;; Support -v flag for old versions of aspell.  (Eli Zaretskii)
  158. ;; Clear minibuffer on ^G from ispell-help (Tak Ota)
  159.  
  160. ;; Revision 3.4 2000/8/4 09:41:50    kss
  161. ;; Support new color display functions.
  162. ;; Fixed misalignment offset bug when replacing a string after a shift made.
  163. ;; Set to standard Author/Maintainer heading,
  164. ;; ensure localwords lists are separated from the text by newline. (Dave Love)
  165. ;; Added dictionary definition for Italian (William Deakin)
  166. ;; HTML region skipping greatly improved. (Chuck D. Phillips)
  167. ;; improved menus.  Fixed regexp matching http/email addresses.
  168. ;; one arg always for XEmacs sleep-for (gunnar Evermann)
  169. ;; support for synchronous processes (Eli Zaretskii)
  170.  
  171. ;; Revision 3.3  1999/11/29 11:38:34     kss
  172. ;; Only word replacements entered in from the keyboard are rechecked.
  173. ;; This fixes a bug in tex parsing and misalignment.
  174. ;; Exceptions exist for recursive edit and query-replace, with tex error
  175. ;; condition tested.  Recursive editing improved.
  176. ;; XEmacs repair for when `enable-multibyte-characters' defined - Didier Verna.
  177. ;; ispell-help fixed for XEmacs.  Choices minibuffer now displayed in XEmacs.
  178. ;; Only list valid dictionaries in Spell menu.  Russian dictionary doesn't allow
  179. ;; run-together words, and uses koi8-r font.  Don't skip text in html <TT>
  180. ;; fonts.
  181.  
  182. ;; Revision 3.2  1999/5/7 14:25:14    kss
  183. ;; Accept ispell versions 3.X.Y where X>=1
  184. ;; fine tuned latex region skipping.  Fixed bug in ispell-word that did not
  185. ;; point in right place on words < 2 chars.  Simplified ispell-minor-mode.
  186. ;; Fixed bug in TeX parsing when math commands are in the comments.
  187. ;; Removed calls to `when' macro.
  188.  
  189. ;; Revision 3.1  1998/12/1 13:21:52    kss
  190. ;; Improved and fixed customize support.
  191. ;; Improved and fixed comments in variables and messages.
  192. ;; A coding system is now required for all languages.
  193. ;; casechars improved for castellano, castellano8, and norsk dictionaries.
  194. ;; Dictionary norsk7-tex removed.  Dictionary polish added.
  195. ;; Dictionaries redefined at load-time to support dictionary changes.
  196. ;; Menu redefined at load time to support dictionary changes.
  197. ;; ispell-check-version added as an alias for `check-ispell-version'.
  198. ;; Spelling suggestions returned in order generated by ispell.
  199. ;; Small bug fixed in matching ispell error messages.
  200. ;; Robustness added to ensure `case-fold-search' doesn't get redefined.
  201. ;; Fixed bug that didn't respect case of word in `ispell-complete-word'.
  202. ;; Multibyte character coding support added for process interactions.
  203. ;; Ensure ispell process has terminated before starting new process.
  204. ;;  This can otherwise confuse process filters and hang ispell.
  205. ;; Improved skipping support for SGML.
  206. ;; Fixed bug using ^M rather than \r in `ispell-minor-check'.
  207. ;; Improved message reference matching in `ispell-message'.
  208. ;; Fixed bug in returning to nroff mode from tex mode.
  209.  
  210. ;;; Compatibility code for xemacs and (not too) older emacsen:
  211.  
  212. (eval-and-compile ;; Protect against declare-function undefined in xemacs
  213.   (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
  214.  
  215. (declare-function ispell-check-minver "ispell" (v1 v2))
  216.  
  217. (if (fboundp 'version<=)
  218.     (defalias 'ispell-check-minver 'version<=)
  219.   (defun ispell-check-minver (minver version)
  220.     "Check if string VERSION is at least string MINVER.
  221. Both must be in [0-9]+.[0-9]+... format.  This is a fallback
  222. compatibility function in case `version<=' is not available."
  223.     (let ((pending t)
  224.       (return t)
  225.       start-ver start-mver)
  226.       ;; Loop until an absolute greater or smaller condition is reached
  227.       ;; or until no elements are left in any of version and minver. In
  228.       ;; this case version is exactly the minimal, so return OK.
  229.       (while pending
  230.     (let (ver mver)
  231.       (if (string-match "[0-9]+" version start-ver)
  232.           (setq start-ver (match-end 0)
  233.             ver (string-to-number (substring version (match-beginning 0) (match-end 0)))))
  234.       (if (string-match "[0-9]+" minver start-mver)
  235.           (setq start-mver (match-end 0)
  236.             mver (string-to-number (substring minver (match-beginning 0) (match-end 0)))))
  237.  
  238.       (if (or ver mver)
  239.           (progn
  240.         (or ver  (setq ver 0))
  241.         (or mver (setq mver 0))
  242.         ;; If none of below conditions match, this element is the
  243.         ;; same. Go checking next element.
  244.         (if (> ver mver)
  245.             (setq pending nil)
  246.           (if (< ver mver)
  247.               (setq pending nil
  248.                 return nil))))
  249.         (setq pending nil))))
  250.       return)))
  251.  
  252. ;;; Code:
  253.  
  254. (defvar mail-yank-prefix)
  255.  
  256. (defgroup ispell nil
  257.   "User variables for Emacs ispell interface."
  258.   :group 'applications)
  259.  
  260. (if (not (fboundp 'buffer-substring-no-properties))
  261.     (defun buffer-substring-no-properties (start end)
  262.       (buffer-substring start end)))
  263.  
  264. (defalias 'check-ispell-version 'ispell-check-version)
  265.  
  266. ;;; **********************************************************************
  267. ;;; The following variables should be set according to personal preference
  268. ;;; and location of binaries:
  269. ;;; **********************************************************************
  270.  
  271.  
  272. ;;;  ******* THIS FILE IS WRITTEN FOR ISPELL VERSION 3.1+
  273.  
  274. (defcustom ispell-highlight-p 'block
  275.   "*Highlight spelling errors when non-nil.
  276. When set to `block', assumes a block cursor with TTY displays."
  277.   :type '(choice (const block) (const :tag "off" nil) (const :tag "on" t))
  278.   :group 'ispell)
  279.  
  280. (defcustom ispell-lazy-highlight (boundp 'lazy-highlight-cleanup)
  281.   "*Controls the lazy-highlighting of spelling errors.
  282. When non-nil, all text in the buffer matching the current spelling
  283. error is highlighted lazily using isearch lazy highlighting (see
  284. `lazy-highlight-initial-delay' and `lazy-highlight-interval')."
  285.   :type 'boolean
  286.   :group 'lazy-highlight
  287.   :group 'ispell
  288.   :version "22.1")
  289.  
  290. (defcustom ispell-highlight-face (if ispell-lazy-highlight 'isearch 'highlight)
  291.   "*The face used for Ispell highlighting.  For Emacsen with overlays.
  292. Possible values are `highlight', `modeline', `secondary-selection',
  293. `region', and `underline'.
  294. This variable can be set by the user to whatever face they desire.
  295. It's most convenient if the cursor color and highlight color are
  296. slightly different."
  297.   :type 'face
  298.   :group 'ispell)
  299.  
  300. (defcustom ispell-check-comments t
  301.   "*Spelling of comments checked when non-nil.
  302. When set to `exclusive', ONLY comments are checked.  (For code comments).
  303. Warning!  Not checking comments, when a comment start is embedded in strings,
  304. may produce undesired results."
  305.   :type '(choice (const exclusive) (const :tag "off" nil) (const :tag "on" t))
  306.   :group 'ispell)
  307. ;;;###autoload(put 'ispell-check-comments 'safe-local-variable (lambda (a) (memq a '(nil t exclusive))))
  308.  
  309. (defcustom ispell-query-replace-choices nil
  310.   "*Corrections made throughout region when non-nil.
  311. Uses `query-replace' (\\[query-replace]) for corrections."
  312.   :type 'boolean
  313.   :group 'ispell)
  314.  
  315. (defcustom ispell-skip-tib nil
  316.   "*Does not spell check `tib' bibliography references when non-nil.
  317. Skips any text between strings matching regular expressions
  318. `ispell-tib-ref-beginning' and `ispell-tib-ref-end'.
  319.  
  320. TeX users beware:  Any field starting with [. will skip until a .] -- even
  321. your whole buffer -- unless you set `ispell-skip-tib' to nil.  That includes
  322. a [.5mm] type of number...."
  323.   :type 'boolean
  324.   :group 'ispell)
  325.  
  326. (defvar ispell-tib-ref-beginning "[[<]\\."
  327.   "Regexp matching the beginning of a Tib reference.")
  328.  
  329. (defvar ispell-tib-ref-end "\\.[]>]"
  330.   "Regexp matching the end of a Tib reference.")
  331.  
  332. (defcustom ispell-keep-choices-win t
  333.   "*When non-nil, the `*Choices*' window remains for spelling session.
  334. This minimizes redisplay thrashing."
  335.   :type 'boolean
  336.   :group 'ispell)
  337.  
  338. (defcustom ispell-choices-win-default-height 2
  339.   "*The default size of the `*Choices*' window, including mode line.
  340. Must be greater than 1."
  341.   :type 'integer
  342.   :group 'ispell)
  343.  
  344. (defcustom ispell-program-name
  345.   (if (executable-find "aspell")
  346.       "aspell"
  347.     (if (executable-find "ispell")
  348.     "ispell"
  349.       (if (executable-find "hunspell")
  350.       "hunspell"
  351.     "ispell")))
  352.   "Program invoked by \\[ispell-word] and \\[ispell-region] commands."
  353.   :type 'string
  354.   :group 'ispell)
  355.  
  356. (defcustom ispell-alternate-dictionary
  357.   (cond ((file-exists-p "/usr/dict/web2") "/usr/dict/web2")
  358.     ((file-exists-p "/usr/share/dict/web2") "/usr/share/dict/web2")
  359.     ((file-exists-p "/usr/dict/words") "/usr/dict/words")
  360.     ((file-exists-p "/usr/lib/dict/words") "/usr/lib/dict/words")
  361.     ((file-exists-p "/usr/share/dict/words") "/usr/share/dict/words")
  362.     ((file-exists-p "/usr/share/lib/dict/words")
  363.      "/usr/share/lib/dict/words")
  364.     ((file-exists-p "/sys/dict") "/sys/dict")
  365.     (t "/usr/dict/words"))
  366.   "*Alternate dictionary for spelling help."
  367.   :type '(choice file (const :tag "None" nil))
  368.   :group 'ispell)
  369.  
  370. (defcustom ispell-complete-word-dict ispell-alternate-dictionary
  371.   "*Dictionary used for word completion."
  372.   :type '(choice file (const :tag "None" nil))
  373.   :group 'ispell)
  374.  
  375. (defcustom ispell-message-dictionary-alist nil
  376.   "*List used by `ispell-message' to select a new dictionary.
  377. It consists of pairs (REGEXP . DICTIONARY).  If REGEXP is found
  378. in the message headers, `ispell-local-dictionary' will be set to
  379. DICTIONARY if `ispell-local-dictionary' is not buffer-local.
  380. E.g. you may use the following value:
  381.   '((\"^Newsgroups:[ \\t]*de\\\\.\" . \"deutsch8\")
  382.     (\"^To:[^\\n,]+\\\\.de[ \\t\\n,>]\" . \"deutsch8\"))"
  383.   :type '(repeat (cons regexp string))
  384.   :group 'ispell)
  385.  
  386.  
  387. (defcustom ispell-message-fcc-skip 50000
  388.   "*Query before saving Fcc message copy if attachment larger than this value.
  389. Always stores Fcc copy of message when nil."
  390.   :type '(choice integer (const :tag "off" nil))
  391.   :group 'ispell)
  392.  
  393.  
  394. (defcustom ispell-grep-command
  395.   ;; MS-Windows/MS-DOS have `egrep' as a Unix shell script, so they
  396.   ;; cannot invoke it.  Use "grep -E" instead (see ispell-grep-options
  397.   ;; below).
  398.   (if (memq system-type '(windows-nt ms-dos)) "grep" "egrep")
  399.   "Name of the grep command for search processes."
  400.   :type 'string
  401.   :group 'ispell)
  402.  
  403. (defcustom ispell-grep-options
  404.   (if (memq system-type '(windows-nt ms-dos)) "-Ei" "-i")
  405.   "String of options to use when running the program in `ispell-grep-command'.
  406. Should probably be \"-i\" or \"-e\".
  407. Some machines (like the NeXT) don't support \"-i\""
  408.   :type 'string
  409.   :group 'ispell)
  410.  
  411. (defcustom ispell-look-command
  412.   (cond ((file-exists-p "/bin/look") "/bin/look")
  413.     ((file-exists-p "/usr/local/bin/look") "/usr/local/bin/look")
  414.     ((file-exists-p "/usr/bin/look") "/usr/bin/look")
  415.     (t "look"))
  416.   "Name of the look command for search processes.
  417. This must be an absolute file name."
  418.   :type 'file
  419.   :group 'ispell)
  420.  
  421. (defcustom ispell-look-p (file-exists-p ispell-look-command)
  422.   "*Non-nil means use `look' rather than `grep'.
  423. Default is based on whether `look' seems to be available."
  424.   :type 'boolean
  425.   :group 'ispell)
  426.  
  427. (defcustom ispell-have-new-look nil
  428.   "*Non-nil means use the `-r' option (regexp) when running `look'."
  429.   :type 'boolean
  430.   :group 'ispell)
  431.  
  432. (defcustom ispell-look-options (if ispell-have-new-look "-dfr" "-df")
  433.   "String of command options for `ispell-look-command'."
  434.   :type 'string
  435.   :group 'ispell)
  436.  
  437. (defcustom ispell-use-ptys-p nil
  438.   "When non-nil, Emacs uses ptys to communicate with Ispell.
  439. When nil, Emacs uses pipes."
  440.   :type 'boolean
  441.   :group 'ispell)
  442.  
  443. (defcustom ispell-following-word nil
  444.   "*Non-nil means `ispell-word' checks the word around or after point.
  445. Otherwise `ispell-word' checks the preceding word."
  446.   :type 'boolean
  447.   :group 'ispell)
  448.  
  449. (defcustom ispell-help-in-bufferp nil
  450.   "*Non-nil means display interactive keymap help in a buffer.
  451. The following values are supported:
  452.   nil        Expand the minibuffer and display a short help message
  453.              there for a couple of seconds.
  454.   t          Pop up a new buffer and display a short help message there
  455.              for a couple of seconds.
  456.   electric   Pop up a new buffer and display a long help message there.
  457.              User can browse and then exit the help mode."
  458.   :type '(choice (const electric) (const :tag "off" nil) (const :tag "on" t))
  459.   :group 'ispell)
  460.  
  461. (defcustom ispell-quietly nil
  462.   "*Non-nil means suppress messages in `ispell-word'."
  463.   :type 'boolean
  464.   :group 'ispell)
  465.  
  466. (defcustom ispell-format-word-function (function upcase)
  467.   "*Formatting function for displaying word being spell checked.
  468. The function must take one string argument and return a string."
  469.   :type 'function
  470.   :group 'ispell)
  471. (if (fboundp 'defvaralias)
  472.     (defvaralias 'ispell-format-word 'ispell-format-word-function))
  473.  
  474. (defcustom ispell-use-framepop-p nil
  475.   "When non-nil ispell uses framepop to display choices in a dedicated frame.
  476. You can set this variable to dynamically use framepop if you are in a
  477. window system by evaluating the following on startup to set this variable:
  478.   (and window-system (condition-case () (require 'framepop) (error nil)))"
  479.   :type 'boolean
  480.   :group 'ispell)
  481.  
  482. ;;;###autoload
  483. (defcustom ispell-personal-dictionary nil
  484.   "*File name of your personal spelling dictionary, or nil.
  485. If nil, the default personal dictionary, (\"~/.ispell_DICTNAME\" for ispell or
  486. \"~/.aspell.LANG.pws\" for aspell) is used, where DICTNAME is the name of your
  487. default dictionary and LANG the two letter language code."
  488.   :type '(choice file
  489.          (const :tag "default" nil))
  490.   :group 'ispell)
  491.  
  492. (defcustom ispell-silently-savep nil
  493.   "*When non-nil, save the personal dictionary without confirmation."
  494.   :type 'boolean
  495.   :group 'ispell)
  496.  
  497. (defvar ispell-local-dictionary-overridden nil
  498.   "Non-nil means the user has explicitly set this buffer's Ispell dictionary.")
  499. (make-variable-buffer-local 'ispell-local-dictionary-overridden)
  500.  
  501. (defcustom ispell-local-dictionary nil
  502.   "If non-nil, the dictionary to be used for Ispell commands in this buffer.
  503. The value must be a string dictionary name,
  504. or nil, which means use the global setting in `ispell-dictionary'.
  505. Dictionary names are defined in `ispell-local-dictionary-alist'
  506. and `ispell-dictionary-alist',
  507.  
  508. Setting `ispell-local-dictionary' to a value has the same effect as
  509. calling \\[ispell-change-dictionary] with that value.  This variable
  510. is automatically set when defined in the file with either
  511. `ispell-dictionary-keyword' or the Local Variable syntax."
  512.   :type '(choice string
  513.          (const :tag "default" nil))
  514.   :group 'ispell)
  515. ;;;###autoload(put 'ispell-local-dictionary 'safe-local-variable 'string-or-null-p)
  516.  
  517. (make-variable-buffer-local 'ispell-local-dictionary)
  518.  
  519. (defcustom ispell-extra-args nil
  520.   "*If non-nil, a list of extra switches to pass to the Ispell program.
  521. For example, (\"-W\" \"3\") to cause it to accept all 1-3 character
  522. words as correct.  See also `ispell-dictionary-alist', which may be used
  523. for language-specific arguments."
  524.   :type '(repeat string)
  525.   :group 'ispell)
  526.  
  527.  
  528.  
  529. (defcustom ispell-skip-html 'use-mode-name
  530.   "*Indicates whether ispell should skip spell checking of SGML markup.
  531. If t, always skip SGML markup; if nil, never skip; if non-t and non-nil,
  532. guess whether SGML markup should be skipped according to the name of the
  533. buffer's major mode."
  534.   :type '(choice (const :tag "always" t) (const :tag "never" nil)
  535.          (const :tag "use-mode-name" use-mode-name))
  536.   :group 'ispell)
  537.  
  538. (make-variable-buffer-local 'ispell-skip-html)
  539.  
  540.  
  541. (defcustom ispell-local-dictionary-alist nil
  542.   "*List of local or customized dictionary definitions.
  543. These can override the values in `ispell-dictionary-alist'.
  544.  
  545. To make permanent changes to your dictionary definitions, you
  546. will need to make your changes in this variable, save, and then
  547. re-start Emacs."
  548.   :type '(repeat (list (choice :tag "Dictionary"
  549.                    (string :tag "Dictionary name")
  550.                    (const :tag "default" nil))
  551.                (regexp :tag "Case characters")
  552.                (regexp :tag "Non case characters")
  553.                (regexp :tag "Other characters")
  554.                (boolean :tag "Many other characters")
  555.                (repeat :tag "Ispell command line args"
  556.                    (string :tag "Arg"))
  557.                (choice :tag "Extended character mode"
  558.                    (const "~tex") (const "~plaintex")
  559.                    (const "~nroff") (const "~list")
  560.                    (const "~latin1") (const "~latin3")
  561.                     (const :tag "default" nil))
  562.                (coding-system :tag "Coding System")))
  563.   :group 'ispell)
  564.  
  565.  
  566. (defvar ispell-dictionary-base-alist
  567.   '((nil                                ; default (English.aff)
  568.      "[A-Za-z]" "[^A-Za-z]" "[']" nil ("-B") nil iso-8859-1)
  569.     ("american"                ; Yankee English
  570.      "[A-Za-z]" "[^A-Za-z]" "[']" nil ("-B") nil iso-8859-1)
  571.     ("brasileiro"            ; Brazilian mode
  572.      "[A-Z\301\311\315\323\332\300\310\314\322\331\303\325\307\334\302\312\324a-z\341\351\355\363\372\340\350\354\362\371\343\365\347\374\342\352\364]"
  573.      "[^A-Z\301\311\315\323\332\300\310\314\322\331\303\325\307\334\302\312\324a-z\341\351\355\363\372\340\350\354\362\371\343\365\347\374\342\352\364]"
  574.      "[']" nil nil nil iso-8859-1)
  575.     ("british"                ; British version
  576.      "[A-Za-z]" "[^A-Za-z]" "[']" nil ("-B") nil iso-8859-1)
  577.     ("castellano"            ; Spanish mode
  578.      "[A-Z\301\311\315\321\323\332\334a-z\341\351\355\361\363\372\374]"
  579.      "[^A-Z\301\311\315\321\323\332\334a-z\341\351\355\361\363\372\374]"
  580.      "[-]" nil ("-B") "~tex" iso-8859-1)
  581.     ("castellano8"            ; 8 bit Spanish mode
  582.      "[A-Z\301\311\315\321\323\332\334a-z\341\351\355\361\363\372\374]"
  583.      "[^A-Z\301\311\315\321\323\332\334a-z\341\351\355\361\363\372\374]"
  584.      "[-]" nil ("-B" "-d" "castellano") "~latin1" iso-8859-1)
  585.     ("czech"
  586.      "[A-Za-z\301\311\314\315\323\332\331\335\256\251\310\330\317\253\322\341\351\354\355\363\372\371\375\276\271\350\370\357\273\362]"
  587.      "[^A-Za-z\301\311\314\315\323\332\331\335\256\251\310\330\317\253\322\341\351\354\355\363\372\371\375\276\271\350\370\357\273\362]"
  588.      "" nil ("-B") nil iso-8859-2)
  589.     ("dansk"                ; Dansk.aff
  590.      "[A-Z\306\330\305a-z\346\370\345]" "[^A-Z\306\330\305a-z\346\370\345]"
  591.      "[']" nil ("-C") nil iso-8859-1)
  592.     ("deutsch"                ; Deutsch.aff
  593.      "[a-zA-Z\"]" "[^a-zA-Z\"]" "[']" t ("-C") "~tex" iso-8859-1)
  594.     ("deutsch8"
  595.      "[a-zA-Z\304\326\334\344\366\337\374]"
  596.      "[^a-zA-Z\304\326\334\344\366\337\374]"
  597.      "[']" t ("-C" "-d" "deutsch") "~latin1" iso-8859-1)
  598.     ("english"                ; make English explicitly selectable
  599.      "[A-Za-z]" "[^A-Za-z]" "[']" nil ("-B") nil iso-8859-1)
  600.     ("esperanto"
  601.      "[A-Za-z\246\254\266\274\306\330\335\336\346\370\375\376]"
  602.      "[^A-Za-z\246\254\266\274\306\330\335\336\346\370\375\376]"
  603.      "[-']" t ("-C") "~latin3" iso-8859-3)
  604.     ("esperanto-tex"
  605.      "[A-Za-z^\\]" "[^A-Za-z^\\]"
  606.      "[-'`\"]" t ("-C" "-d" "esperanto") "~tex" iso-8859-3)
  607.     ("francais7"
  608.      "[A-Za-z]" "[^A-Za-z]" "[`'^-]" t nil nil iso-8859-1)
  609.     ("francais"                ; Francais.aff
  610.      "[A-Za-z\300\302\306\307\310\311\312\313\316\317\324\331\333\334\340\342\347\350\351\352\353\356\357\364\371\373\374]"
  611.      "[^A-Za-z\300\302\306\307\310\311\312\313\316\317\324\331\333\334\340\342\347\350\351\352\353\356\357\364\371\373\374]"
  612.      "[-'.@]" t nil "~list" iso-8859-1)
  613.     ("francais-tex"            ; Francais.aff
  614.      "[A-Za-z\300\302\306\307\310\311\312\313\316\317\324\331\333\334\340\342\347\350\351\352\353\356\357\364\371\373\374\\]"
  615.      "[^A-Za-z\300\302\306\307\310\311\312\313\316\317\324\331\333\334\340\342\347\350\351\352\353\356\357\364\371\373\374\\]"
  616.      "[-'^`\".@]" t nil "~tex" iso-8859-1)
  617.     ("german"                ; german.aff
  618.      "[a-zA-Z\"]" "[^a-zA-Z\"]" "[']" t ("-C") "~tex" iso-8859-1)
  619.     ("german8"                ; german.aff
  620.      "[a-zA-Z\304\326\334\344\366\337\374]"
  621.      "[^a-zA-Z\304\326\334\344\366\337\374]"
  622.      "[']" t ("-C" "-d" "german") "~latin1" iso-8859-1)
  623.     ("italiano"                ; Italian.aff
  624.      "[A-Z\300\301\310\311\314\315\322\323\331\332a-z\340\341\350\351\354\355\363\371\372]"
  625.      "[^A-Z\300\301\310\311\314\315\322\323\331\332a-z\340\341\350\351\354\355\363\371\372]"
  626.      "[-.]" nil ("-B" "-d" "italian") "~tex" iso-8859-1)
  627.     ("nederlands"            ; Nederlands.aff
  628.      "[A-Za-z\300\301\302\303\304\305\307\310\311\312\313\314\315\316\317\322\323\324\325\326\331\332\333\334\340\341\342\343\344\345\347\350\351\352\353\354\355\356\357\361\362\363\364\365\366\371\372\373\374]"
  629.      "[^A-Za-z\300\301\302\303\304\305\307\310\311\312\313\314\315\316\317\322\323\324\325\326\331\332\333\334\340\341\342\343\344\345\347\350\351\352\353\354\355\356\357\361\362\363\364\365\366\371\372\373\374]"
  630.      "[']" t ("-C") nil iso-8859-1)
  631.     ("nederlands8"            ; Dutch8.aff
  632.      "[A-Za-z\300\301\302\303\304\305\307\310\311\312\313\314\315\316\317\322\323\324\325\326\331\332\333\334\340\341\342\343\344\345\347\350\351\352\353\354\355\356\357\361\362\363\364\365\366\371\372\373\374]"
  633.      "[^A-Za-z\300\301\302\303\304\305\307\310\311\312\313\314\315\316\317\322\323\324\325\326\331\332\333\334\340\341\342\343\344\345\347\350\351\352\353\354\355\356\357\361\362\363\364\365\366\371\372\373\374]"
  634.      "[']" t ("-C") nil iso-8859-1)
  635.     ("norsk"                ; 8 bit Norwegian mode
  636.      "[A-Za-z\305\306\307\310\311\322\324\330\345\346\347\350\351\362\364\370]"
  637.      "[^A-Za-z\305\306\307\310\311\322\324\330\345\346\347\350\351\362\364\370]"
  638.      "[\"]" nil nil "~list" iso-8859-1)
  639.     ("norsk7-tex"            ; 7 bit Norwegian TeX mode
  640.      "[A-Za-z{}\\'^`]" "[^A-Za-z{}\\'^`]"
  641.      "[\"]" nil ("-d" "norsk") "~plaintex" iso-8859-1)
  642.     ("polish"                ; Polish mode
  643.      "[A-Za-z\241\243\246\254\257\261\263\266\274\277\306\312\321\323\346\352\361\363]"
  644.      "[^A-Za-z\241\243\246\254\257\261\263\266\274\277\306\312\321\323\346\352\361\363]"
  645.      "[.]" nil nil nil iso-8859-2)
  646.     ("portugues"                        ; Portuguese mode
  647.      "[a-zA-Z\301\302\311\323\340\341\342\351\352\355\363\343\372]"
  648.      "[^a-zA-Z\301\302\311\323\340\341\342\351\352\355\363\343\372]"
  649.      "[']" t ("-C") "~latin1" iso-8859-1)
  650.     ("russian"                ; Russian.aff (KOI8-R charset)
  651.      "[\341\342\367\347\344\345\263\366\372\351\352\353\354\355\356\357\360\362\363\364\365\346\350\343\376\373\375\370\371\377\374\340\361\301\302\327\307\304\305\243\326\332\311\312\313\314\315\316\317\320\322\323\324\325\306\310\303\336\333\335\330\331\337\334\300\321]"
  652.      "[^\341\342\367\347\344\345\263\366\372\351\352\353\354\355\356\357\360\362\363\364\365\346\350\343\376\373\375\370\371\377\374\340\361\301\302\327\307\304\305\243\326\332\311\312\313\314\315\316\317\320\322\323\324\325\306\310\303\336\333\335\330\331\337\334\300\321]"
  653.      "" nil nil nil koi8-r)
  654.     ("russianw"                ; russianw.aff (CP1251 charset)
  655.      "[\300\301\302\303\304\305\250\306\307\310\311\312\313\314\315\316\317\320\321\322\323\324\325\326\327\330\331\334\333\332\335\336\337\340\341\342\343\344\345\270\346\347\350\351\352\353\354\355\356\357\360\361\362\363\364\365\366\367\370\371\374\373\372\375\376\377]"
  656.      "[^\300\301\302\303\304\305\250\306\307\310\311\312\313\314\315\316\317\320\321\322\323\324\325\326\327\330\331\334\333\332\335\336\337\340\341\342\343\344\345\270\346\347\350\351\352\353\354\355\356\357\360\361\362\363\364\365\366\367\370\371\374\373\372\375\376\377]"
  657.      "" nil nil nil windows-1251)
  658.     ("slovak"                ; Slovakian
  659.      "[A-Za-z\301\304\311\315\323\332\324\300\305\245\335\256\251\310\317\253\322\341\344\351\355\363\372\364\340\345\265\375\276\271\350\357\273\362]"
  660.      "[^A-Za-z\301\304\311\315\323\332\324\300\305\245\335\256\251\310\317\253\322\341\344\351\355\363\372\364\340\345\265\375\276\271\350\357\273\362]"
  661.      "" nil ("-B") nil iso-8859-2)
  662.     ("slovenian"                        ; Slovenian
  663.      "[A-Za-z\301\304\311\315\323\332\324\300\305\245\335\256\251\310\317\253\322\341\344\351\355\363\372\364\340\345\265\375\276\271\350\357\273\362]"
  664.      "[^A-Za-z\301\304\311\315\323\332\324\300\305\245\335\256\251\310\317\253\322\341\344\351\355\363\372\364\340\345\265\375\276\271\350\357\273\362]"
  665.      "" nil ("-B" "-d" "slovenian") nil iso-8859-2)
  666.     ("svenska"                ; Swedish mode
  667.      "[A-Za-z\345\344\366\351\340\374\350\346\370\347\305\304\326\311\300\334\310\306\330\307]"
  668.      "[^A-Za-z\345\344\366\351\340\374\350\346\370\347\305\304\326\311\300\334\310\306\330\307]"
  669.      "[']" nil ("-C") "~list" iso-8859-1))
  670.   "Base value for `ispell-dictionary-alist'.")
  671.  
  672. (defvar ispell-dictionary-alist nil
  673.   "An alist of dictionaries and their associated parameters.
  674.  
  675. Each element of this list is also a list:
  676.  
  677. \(DICTIONARY-NAME CASECHARS NOT-CASECHARS OTHERCHARS MANY-OTHERCHARS-P
  678.         ISPELL-ARGS EXTENDED-CHARACTER-MODE CHARACTER-SET\)
  679.  
  680. DICTIONARY-NAME is a possible string value of variable `ispell-dictionary',
  681. nil means the default dictionary.
  682.  
  683. CASECHARS is a regular expression of valid characters that comprise a word.
  684.  
  685. NOT-CASECHARS is the opposite regexp of CASECHARS.
  686.  
  687. OTHERCHARS is a regexp of characters in the NOT-CASECHARS set but which can be
  688. used to construct words in some special way.  If OTHERCHARS characters follow
  689. and precede characters from CASECHARS, they are parsed as part of a word,
  690. otherwise they become word-breaks.  As an example in English, assume the
  691. regular expression \"[']\" for OTHERCHARS.  Then \"they're\" and
  692. \"Steven's\" are parsed as single words including the \"'\" character, but
  693. \"Stevens'\" does not include the quote character as part of the word.
  694. If you want OTHERCHARS to be empty, use the empty string.
  695. Hint: regexp syntax requires the hyphen to be declared first here.
  696.  
  697. CASECHARS, NOT-CASECHARS, and OTHERCHARS must be unibyte strings
  698. containing bytes of CHARACTER-SET.  In addition, if they contain
  699. a non-ASCII byte, the regular expression must be a single
  700. `character set' construct that doesn't specify a character range
  701. for non-ASCII bytes.
  702.  
  703. MANY-OTHERCHARS-P is non-nil when multiple OTHERCHARS are allowed in a word.
  704. Otherwise only a single OTHERCHARS character is allowed to be part of any
  705. single word.
  706.  
  707. ISPELL-ARGS is a list of additional arguments passed to the ispell
  708. subprocess.
  709.  
  710. EXTENDED-CHARACTER-MODE should be used when dictionaries are used which
  711. have been configured in an Ispell affix file.  (For example, umlauts
  712. can be encoded as \\\"a, a\\\", \"a, ...)  Defaults are ~tex and ~nroff
  713. in English.  This has the same effect as the command-line `-T' option.
  714. The buffer Major Mode controls Ispell's parsing in tex or nroff mode,
  715. but the dictionary can control the extended character mode.
  716. Both defaults can be overruled in a buffer-local fashion. See
  717. `ispell-parsing-keyword' for details on this.
  718.  
  719. CHARACTER-SET used for languages with multibyte characters.
  720.  
  721. Note that the CASECHARS and OTHERCHARS slots of the alist should
  722. contain the same character set as casechars and otherchars in the
  723. LANGUAGE.aff file \(e.g., english.aff\).")
  724.  
  725. (defvar ispell-really-aspell nil)   ; Non-nil if aspell extensions should be used
  726. (defvar ispell-really-hunspell nil) ; Non-nil if hunspell extensions should be used
  727. (defvar ispell-encoding8-command nil
  728.   "Command line option prefix to select UTF-8 if supported, nil otherwise.
  729. If UTF-8 if supported by spellchecker and is selectable from the command line
  730. this variable will contain \"--encoding=\" for aspell and \"-i \" for hunspell,
  731. so UTF-8 or other mime charsets can be selected. That will be set for hunspell
  732. >=1.1.6 or aspell >= 0.60 in `ispell-check-version'.
  733.  
  734. For aspell non-nil means to try to automatically find aspell dictionaries.
  735. Earlier aspell versions do not consistently support UTF-8.  Handling
  736. this would require some extra guessing in `ispell-aspell-find-dictionary'.")
  737.  
  738. (defvar ispell-aspell-supports-utf8 nil
  739.   "Non nil if aspell has consistent command line UTF-8 support. Obsolete.
  740. ispell.el and flyspell.el will use for this purpose the more generic
  741. variable `ispell-encoding8-command' for both aspell and hunspell. Is left
  742. here just for backwards compatibility.")
  743.  
  744. (make-obsolete-variable 'ispell-aspell-supports-utf8
  745.                         'ispell-encoding8-command "23.1")
  746.  
  747.  
  748. ;;; **********************************************************************
  749. ;;; The following are used by ispell, and should not be changed.
  750. ;;; **********************************************************************
  751.  
  752. ;; -----------------------------------------------------------------------
  753. ;; Debian changes
  754. ;;
  755.  
  756. ;; Obtain buffer encoding in the format aspell prefers
  757.  
  758. (defun ispell-get-mime-charset ()
  759.   "Obtain buffer encoding in a format aspell understands"
  760.   (condition-case ()
  761.       (if (featurep 'xemacs)
  762.       (coding-system-name
  763.        (coding-system-base buffer-file-coding-system))
  764.     (coding-system-get buffer-file-coding-system 'mime-charset))
  765.     (error nil)))
  766.  
  767. ;; Try to obtain a list of supported aspell charsets
  768.  
  769. (defun ispell-get-aspell-encodings ()
  770.   (let ((encodings '(utf-8)))
  771.     (dolist (file (directory-files "/usr/lib/aspell" nil ".*\\.cset$" 'nosort))
  772.       (string-match "\\.cset$" file)
  773.       (add-to-list 'encodings (replace-match "" t t file )))
  774.     encodings))
  775.  
  776. ;; Define again this defcustom, so it takes care of initializing
  777. ;; spellchecker params after redefinition if debian-ispell.el is loaded
  778. ;; featurep is used to protect against some autoload loops
  779.  
  780. (defcustom ispell-program-name "ispell"
  781.   "Program invoked by \\[ispell-word] and \\[ispell-region] commands."
  782.   :type 'string
  783.   :set (lambda (symbol value)
  784.          (set-default symbol value)
  785.          (if (featurep 'ispell)
  786.              (ispell-set-spellchecker-params)))
  787.   :group 'ispell)
  788.  
  789. ;; These defs are already in debian-ispell.el, but since both files
  790. ;; might be used separately is better to have them here too.
  791.  
  792. (defcustom debian-dict-common-debug nil
  793.   "A lot of debugging info will be shown if non nil."
  794.   :type 'boolean
  795.   :group 'ispell)
  796.  
  797. (defvar debian-ispell-valid-dictionary-list nil
  798.   "A list that will contain the list of emacsen names provided by
  799. registered ispell or aspell dicts")
  800.  
  801. ;; Make sure debian-ispell-program-name is not void, but initially
  802. ;; set to ispell-program-name. Currently unused.
  803.  
  804. (if (not (boundp 'debian-ispell-program-name))
  805.     (setq debian-ispell-program-name ispell-program-name))
  806.  
  807. ;; xemacs21 has match.string-no-properties hidden in xml.el
  808.  
  809. (or (fboundp 'match-string-no-properties)
  810.     (require 'xml))
  811.  
  812. ;; Define cp1251 as an alias to windows-1251 for xemacs
  813.  
  814. (if (featurep 'xemacs)
  815.     (condition-case ()
  816.     (define-coding-system-alias 'cp1251 'windows-1251)
  817.       (error nil)))
  818.  
  819. ;;; xemacs21 and emacs21 do not have with-no-warnings
  820.  
  821. (if (fboundp 'with-no-warnings)
  822.     (defalias 'ispell-with-no-warnings 'with-no-warnings)
  823.   (defun ispell-with-no-warnings (&rest body)
  824.     "Like `progn', but prevents compiler warnings in the body."
  825.     ;; Taken from Emacs' byte-run.el
  826.     ;; The implementation for the interpreter is basically trivial.
  827.     (car (last body))))
  828.  
  829. ;; plain delete-process does not work well in emacs21 due to a race condition.
  830.  
  831. (defun ispell-delete-ispell-process ()
  832.   "Workaround race condition in emacs21 that makes delete-process not work well"
  833.   (if (not (member debian-emacs-flavor '(emacs20 emacs21)))
  834.       (delete-process ispell-process)
  835.     (process-send-eof ispell-process)
  836.     (if (eq (ispell-process-status) 'run)
  837.         (ispell-accept-output 1))
  838.     (if (eq (ispell-process-status) 'run)
  839.         (kill-process ispell-process))
  840.     (while (not (or (eq (ispell-process-status) 'exit)
  841.                     (eq (ispell-process-status) 'signal)))
  842.       (if (or xemacsp version20p) (sleep-for 0.25)
  843.         (sleep-for 0 250)))))
  844.  
  845. ;;; End of Debian changes
  846. ;;; -----------------------------------------------------------------------
  847.  
  848. ;;; The version must be 3.1 or greater for this version of ispell.el
  849. ;;; There is an incompatibility between version 3.1.12 and lower versions.
  850. (defconst ispell-required-version '(3 1 12)
  851.   "Ispell versions with which this version of ispell.el is known to work.")
  852. (defvar ispell-offset -1
  853.   "Offset that maps protocol differences between ispell 3.1 versions.")
  854.  
  855. (defconst ispell-version "ispell.el 3.6 - 7-Jan-2003")
  856.  
  857. ;;; -----------------------------------------------------------------------
  858. ;;; Tell people that this version has been patched for the Debian system
  859. ;;; (dictionaries-common package)
  860.  
  861. (defconst ispell-version (concat ispell-version " (+ Debian `dictionaries-common' changes)"))
  862.  
  863. ;;; End of Debian changes
  864. ;;; -----------------------------------------------------------------------
  865.  
  866. (defun ispell-check-version (&optional interactivep)
  867.   "Ensure that `ispell-program-name' is valid and the correct version.
  868. Returns version number if called interactively.
  869. Otherwise returns the library directory name, if that is defined."
  870.   ;; This is a little wasteful as we actually launch ispell twice: once
  871.   ;; to make sure it's the right version, and once for real.  But people
  872.   ;; get confused by version mismatches *all* the time (and I've got the
  873.   ;; email to prove it) so I think this is worthwhile.  And the -v[ersion]
  874.   ;; option is the only way I can think of to do this that works with
  875.   ;; all versions, since versions earlier than 3.0.09 didn't identify
  876.   ;; themselves on startup.
  877.   (interactive "p")
  878.   (let (;; avoid bugs when syntax of `.' changes in various default modes
  879.     (default-major-mode 'fundamental-mode)
  880.     (default-directory (or (and (boundp 'temporary-file-directory)
  881.                     temporary-file-directory)
  882.                    default-directory))
  883.     result status ispell-program-version)
  884.  
  885.     (with-temp-buffer
  886.       (setq status (ispell-call-process
  887.             ispell-program-name nil t nil
  888.             ;; aspell doesn't accept the -vv switch.
  889.             (let ((case-fold-search
  890.                (memq system-type '(ms-dos windows-nt)))
  891.               (speller
  892.                (file-name-nondirectory ispell-program-name)))
  893.               ;; Assume anything that isn't `aspell' is Ispell.
  894.               (if (string-match "\\`aspell" speller) "-v" "-vv"))))
  895.       (goto-char (point-min))
  896.       (if interactivep
  897.       ;; report version information of ispell and ispell.el
  898.       (progn
  899.         (end-of-line)
  900.         (setq result (concat (buffer-substring-no-properties (point-min)
  901.                                  (point))
  902.                  ", "
  903.                  ispell-version))
  904.         (message "%s" result))
  905.     ;; return library directory.
  906.     (if (re-search-forward "LIBDIR = \\\"\\([^ \t\n]*\\)\\\"" nil t)
  907.         (setq result (buffer-substring (match-beginning 1) (match-end 1)))))
  908.       (goto-char (point-min))
  909.       (if (not (memq status '(0 nil)))
  910.       (error "%s exited with %s %s" ispell-program-name
  911.          (if (stringp status) "signal" "code") status))
  912.  
  913.       ;; Get relevant version strings. Only xx.yy.... format works well
  914.       (let (case-fold-search)
  915.     (setq ispell-program-version
  916.           (and (search-forward-regexp "\\([0-9]+\\.[0-9\\.]+\\)" nil t)
  917.            (match-string 1)))
  918.  
  919.     ;; Make sure these variables are (re-)initialized to the default value
  920.     (setq ispell-really-aspell nil
  921.           ispell-aspell-supports-utf8 nil
  922.           ispell-really-hunspell nil
  923.           ispell-encoding8-command nil)
  924.  
  925.     (goto-char (point-min))
  926.     (or (setq ispell-really-aspell
  927.           (and (search-forward-regexp
  928.             "(but really Aspell \\([0-9]+\\.[0-9\\.]+\\)?)" nil t)
  929.                (match-string 1)))
  930.         (setq ispell-really-hunspell
  931.           (and (search-forward-regexp
  932.             "(but really Hunspell \\([0-9]+\\.[0-9\\.]+\\)?)" nil t)
  933.                (match-string 1)))))
  934.  
  935.       (let ((aspell-minver    "0.50")
  936.         (aspell8-minver   "0.60")
  937.         (ispell0-minver   "3.1.0")
  938.         (ispell-minver    "3.1.12")
  939.         (hunspell8-minver "1.1.6"))
  940.  
  941.     (if (ispell-check-minver ispell0-minver ispell-program-version)
  942.         (or (ispell-check-minver ispell-minver ispell-program-version)
  943.         (setq ispell-offset 0))
  944.       (error "%s release %s or greater is required"
  945.          ispell-program-name
  946.          ispell-minver))
  947.  
  948.     (cond
  949.      (ispell-really-aspell
  950.       (if (ispell-check-minver aspell-minver ispell-really-aspell)
  951.           (if (ispell-check-minver aspell8-minver ispell-really-aspell)
  952.           (progn
  953.             (setq ispell-aspell-supports-utf8 t)
  954.             (setq ispell-encoding8-command "--encoding=")))
  955.         (setq ispell-really-aspell nil)))
  956.      (ispell-really-hunspell
  957.       (if (ispell-check-minver hunspell8-minver ispell-really-hunspell)
  958.           (setq ispell-encoding8-command "-i ")
  959.         (setq ispell-really-hunspell nil))))))
  960.     result))
  961.  
  962. (defun ispell-call-process (&rest args)
  963.   "Like `call-process' but defend against bad `default-directory'."
  964.   (let ((default-directory default-directory))
  965.     (unless (and (file-directory-p default-directory)
  966.          (file-readable-p default-directory))
  967.       (setq default-directory (expand-file-name "~/")))
  968.     (apply 'call-process args)))
  969.  
  970. (defun ispell-call-process-region (&rest args)
  971.   "Like `call-process-region' but defend against bad `default-directory'."
  972.   (let ((default-directory default-directory))
  973.     (unless (and (file-directory-p default-directory)
  974.          (file-readable-p default-directory))
  975.       (setq default-directory (expand-file-name "~/")))
  976.     (apply 'call-process-region args)))
  977.  
  978.  
  979.  
  980. ;;; The preparation of the menu bar menu must be autoloaded
  981. ;;; because otherwise this file gets autoloaded every time Emacs starts
  982. ;;; so that it can set up the menus and determine keyboard equivalents.
  983.  
  984.  
  985. ;;;###autoload
  986. (defvar ispell-menu-map nil "Key map for ispell menu.")
  987. ;;; redo menu when loading ispell to get dictionary modifications
  988. (setq ispell-menu-map nil)
  989.  
  990. ;;;###autoload
  991. (defvar ispell-menu-xemacs nil
  992.   "Spelling menu for XEmacs.
  993. If nil when package is loaded, a standard menu will be set,
  994. and added as a submenu of the \"Edit\" menu.")
  995.  
  996. ;; Break out XEmacs menu and split into several calls to avoid having
  997. ;; long lines in loaddefs.el.  Detect need off following constant.
  998.  
  999. ;;; Set up dictionary
  1000. ;;;###autoload
  1001. (defvar ispell-menu-map-needed
  1002.   ;; only needed when not version 18 and not XEmacs.
  1003.   (and (not ispell-menu-map)
  1004.        (not (featurep 'xemacs))
  1005.        'reload))
  1006.  
  1007. (defvar ispell-library-directory (condition-case ()
  1008.                      (ispell-check-version)
  1009.                    (error nil))
  1010.   "Directory where ispell dictionaries reside.")
  1011.  
  1012. (defvar ispell-process nil
  1013.   "The process object for Ispell.")
  1014.  
  1015. (defvar ispell-async-processp (and (fboundp 'delete-process)
  1016.                    (fboundp 'process-send-string)
  1017.                    (fboundp 'accept-process-output)
  1018.                    ;;(fboundp 'start-process)
  1019.                    ;;(fboundp 'set-process-filter)
  1020.                    ;;(fboundp 'process-kill-without-query)
  1021.                    )
  1022.   "Non-nil means that the OS supports asynchronous processes.")
  1023.  
  1024. ;; Make ispell.el work better with aspell.
  1025.  
  1026. (defvar ispell-aspell-dictionary-alist nil
  1027.   "An alist of parsed aspell dicts and associated parameters.
  1028. Internal use.")
  1029.  
  1030. (defun ispell-find-aspell-dictionaries ()
  1031.   "Find Aspell's dictionaries, and record in `ispell-dictionary-alist'."
  1032.   (unless (and ispell-really-aspell ispell-encoding8-command)
  1033.     (error "This function only works with aspell >= 0.60."))
  1034.   (let* ((dictionaries
  1035.       (split-string
  1036.        (with-temp-buffer
  1037.          (ispell-call-process ispell-program-name nil t nil "dicts")
  1038.          (buffer-string))))
  1039.      ;; Search for the named dictionaries.
  1040.      (found
  1041.       (delq nil
  1042.         (mapcar #'ispell-aspell-find-dictionary dictionaries))))
  1043.     ;; Ensure aspell's alias dictionary will override standard
  1044.     ;; definitions.
  1045.     (setq found (ispell-aspell-add-aliases found))
  1046.     ;; Merge into FOUND any elements from the standard ispell-dictionary-alist
  1047.     ;; which have no element in FOUND at all.
  1048.     (dolist (dict ispell-dictionary-alist)
  1049.       (unless (assoc (car dict) found)
  1050.     (setq found (nconc found (list dict)))))
  1051.     (setq ispell-aspell-dictionary-alist found)
  1052.     ;; Add a default entry
  1053.     (let ((default-dict '(nil "[[:alpha:]]" "[^[:alpha:]]" "[']" nil ("-B") nil utf-8)))
  1054.       (push default-dict ispell-aspell-dictionary-alist))))
  1055.  
  1056. (defvar ispell-aspell-data-dir nil
  1057.   "Data directory of Aspell.")
  1058.  
  1059. (defvar ispell-aspell-dict-dir nil
  1060.   "Dictionary directory of Aspell.")
  1061.  
  1062. (defun ispell-get-aspell-config-value (key)
  1063.   "Return value of Aspell configuration option KEY.
  1064. Assumes that value contains no whitespace."
  1065.   (with-temp-buffer
  1066.     (ispell-call-process ispell-program-name nil t nil "config" key)
  1067.     (car (split-string (buffer-string)))))
  1068.  
  1069. (defun ispell-aspell-find-dictionary (dict-name)
  1070.   ;; This returns nil if the data file does not exist.
  1071.   ;; Can someone please explain the return value format when the
  1072.   ;; file does exist -- rms?
  1073.   (let* ((lang ;; Strip out region, variant, etc.
  1074.       (and (string-match "^[[:alpha:]]+" dict-name)
  1075.            (match-string 0 dict-name)))
  1076.      (data-file
  1077.       (concat (or ispell-aspell-data-dir
  1078.               (setq ispell-aspell-data-dir
  1079.                 (ispell-get-aspell-config-value "data-dir")))
  1080.           "/" lang ".dat"))
  1081.      otherchars)
  1082.     (condition-case ()
  1083.     (with-temp-buffer
  1084.       (insert-file-contents data-file)
  1085.       ;; There is zero or one line with special characters declarations.
  1086.       (when (search-forward-regexp "^special" nil t)
  1087.         (let ((specials (split-string
  1088.                  (buffer-substring (point)
  1089.                            (progn (end-of-line) (point))))))
  1090.           ;; The line looks like: special ' -** - -** . -** : -*-
  1091.           ;; -** means that this character
  1092.           ;;    - doesn't appear at word start
  1093.           ;;    * may appear in the middle of a word
  1094.           ;;    * may appear at word end
  1095.           ;; `otherchars' is about the middle case.
  1096.           (while specials
  1097.         (when (eq (aref (cadr specials) 1) ?*)
  1098.           (push (car specials) otherchars))
  1099.         (setq specials (cddr specials)))))
  1100.       (list dict-name
  1101.         "[[:alpha:]]"
  1102.         "[^[:alpha:]]"
  1103.         (regexp-opt otherchars)
  1104.         t                 ; We can't tell, so set this to t
  1105.         (list "-d" dict-name)
  1106.         nil                ; aspell doesn't support this
  1107.         ;; Here we specify the encoding to use while communicating with
  1108.         ;; aspell.  This doesn't apply to command line arguments, so
  1109.         ;; just don't pass words to spellcheck as arguments...
  1110.         'utf-8))
  1111.       (file-error
  1112.        nil))))
  1113.  
  1114. (defun ispell-aspell-add-aliases (alist)
  1115.   "Find aspell's dictionary aliases and add them to dictionary ALIST.
  1116. Return the new dictionary alist."
  1117.   (let ((aliases (file-expand-wildcards
  1118.           (concat (or ispell-aspell-dict-dir
  1119.                   (setq ispell-aspell-dict-dir
  1120.                     (ispell-get-aspell-config-value "dict-dir")))
  1121.               "/*.alias"))))
  1122.     (dolist (alias-file aliases)
  1123.       (with-temp-buffer
  1124.     (insert-file-contents alias-file)
  1125.     ;; Look for a line "add FOO.multi", extract FOO
  1126.     (when (search-forward-regexp "^add \\([^.]+\\)\\.multi" nil t)
  1127.       (let* ((aliasname (file-name-sans-extension
  1128.                  (file-name-nondirectory alias-file)))
  1129.          (already-exists-p (assoc aliasname alist))
  1130.          (realname (match-string 1))
  1131.          (realdict (assoc realname alist)))
  1132.         (when (and realdict (not already-exists-p))
  1133.           (push (cons aliasname (cdr realdict)) alist))))))
  1134.     alist))
  1135.  
  1136. ;; Set params according to the selected spellchecker
  1137.  
  1138. (defvar ispell-last-program-name nil
  1139.   "Last value of `ispell-program-name'. Internal use.")
  1140.  
  1141. (defvar ispell-initialize-spellchecker-hook nil
  1142.   "Normal hook run on spellchecker initialization.
  1143. This hook is run when a spellchecker is used for the first
  1144. time, before `ispell-dictionary-alist' is set.  It is intended for
  1145. sysadmins to override entries in `ispell-dictionary-base-alist'
  1146. by putting those overrides in `ispell-base-dicts-override-alist', which is
  1147. a dynamically scoped var with same format as `ispell-dictionary-alist'.
  1148. This alist will not override the auto-detected values (e.g. if a recent
  1149. aspell is used along with Emacs).")
  1150.  
  1151. (defun ispell-set-spellchecker-params ()
  1152.   "Initialize some spellchecker parameters when changed or first used."
  1153.   (unless (eq ispell-last-program-name ispell-program-name)
  1154.     (setq ispell-last-program-name ispell-program-name)
  1155.     (ispell-kill-ispell t)
  1156.     (if (and (condition-case ()
  1157.          (progn
  1158.            (setq ispell-library-directory (ispell-check-version))
  1159.            t)
  1160.            (error nil))
  1161.          ispell-really-aspell
  1162.          ispell-encoding8-command
  1163.          ;; XEmacs does not like [:alpha:] regexps.
  1164.          (string-match "^[[:alpha:]]+$" "abcde"))
  1165.     (unless ispell-aspell-dictionary-alist
  1166.       (ispell-find-aspell-dictionaries)))
  1167.  
  1168.     ;; Substitute ispell-dictionary-alist with the list of dictionaries
  1169.     ;; corresponding to the given spellchecker. If a recent aspell, use
  1170.     ;; the list of really installed dictionaries and add to it elements
  1171.     ;; of the original list that are not present there. Allow distro info.
  1172.     (let ((found-dicts-alist
  1173.        (if (and ispell-really-aspell
  1174.             ispell-encoding8-command)
  1175.            ispell-aspell-dictionary-alist
  1176.          nil))
  1177.       ispell-base-dicts-override-alist ; Override only base-dicts-alist
  1178.       all-dicts-alist)
  1179.  
  1180.       (run-hooks 'ispell-initialize-spellchecker-hook)
  1181.  
  1182.       ;; Add dicts to ``ispell-dictionary-alist'' unless already present.
  1183.       (dolist (dict (append found-dicts-alist
  1184.                 ispell-base-dicts-override-alist
  1185.                 ispell-dictionary-base-alist))
  1186.     (unless (assoc (car dict) all-dicts-alist)
  1187.       (add-to-list 'all-dicts-alist dict)))
  1188.       (setq ispell-dictionary-alist all-dicts-alist))))
  1189.  
  1190.  
  1191. (defun ispell-valid-dictionary-list ()
  1192.   "Returns a list of valid dictionaries.
  1193. The variable `ispell-library-directory' defines the library location."
  1194.   ;; Initialize variables and dictionaries alists for desired spellchecker.
  1195.   ;; Make sure ispell.el is loaded to avoid some autoload loops in XEmacs
  1196.   ;; (and may be others)
  1197.   (if (featurep 'ispell)
  1198.       (ispell-set-spellchecker-params))
  1199.  
  1200.   (let ((dicts (append ispell-local-dictionary-alist ispell-dictionary-alist))
  1201.     (dict-list (cons "default" nil))
  1202.     name load-dict)
  1203.     (dolist (dict dicts)
  1204.       (setq name (car dict)
  1205.         load-dict (car (cdr (member "-d" (nth 5 dict)))))
  1206.       ;; Include if the dictionary is in the library, or dir not defined.
  1207.       (if (and
  1208.        name
  1209.        ;; include all dictionaries if lib directory not known.
  1210.        ;; For Aspell, we already know which dictionaries exist.
  1211.        (or ispell-really-aspell
  1212.            (not ispell-library-directory)
  1213.            ;; -- Debian changes: Check if one of the registered dicts
  1214.            (member name debian-ispell-valid-dictionary-list)
  1215.            ;; -- End of debian changes
  1216.            (file-exists-p (concat ispell-library-directory
  1217.                       "/" name ".hash"))
  1218.            (file-exists-p (concat ispell-library-directory "/" name ".has"))
  1219.            (and load-dict
  1220.             (or (file-exists-p (concat ispell-library-directory
  1221.                            "/" load-dict ".hash"))
  1222.             (file-exists-p (concat ispell-library-directory
  1223.                            "/" load-dict ".has"))))))
  1224.       (setq dict-list (cons name dict-list))))
  1225.     dict-list))
  1226.  
  1227. ;;; define commands in menu in opposite order you want them to appear.
  1228. ;;;###autoload
  1229. (if ispell-menu-map-needed
  1230.     (progn
  1231.       (setq ispell-menu-map (make-sparse-keymap "Spell"))
  1232.       (define-key ispell-menu-map [ispell-change-dictionary]
  1233.     '(menu-item "Change Dictionary..." ispell-change-dictionary
  1234.             :help "Supply explicit dictionary file name"))
  1235.       (define-key ispell-menu-map [ispell-kill-ispell]
  1236.     '(menu-item "Kill Process" ispell-kill-ispell
  1237.             :enable (and (boundp 'ispell-process) ispell-process
  1238.                  (eq (ispell-process-status) 'run))
  1239.             :help "Terminate Ispell subprocess"))
  1240.       (define-key ispell-menu-map [ispell-pdict-save]
  1241.     '(menu-item "Save Dictionary"
  1242.             (lambda () (interactive) (ispell-pdict-save t t))
  1243.             :help "Save personal dictionary"))
  1244.       (define-key ispell-menu-map [ispell-customize]
  1245.     '(menu-item "Customize..."
  1246.             (lambda () (interactive) (customize-group 'ispell))
  1247.             :help "Customize spell checking options"))
  1248.       (define-key ispell-menu-map [ispell-help]
  1249.     ;; use (x-popup-menu last-nonmenu-event(list "" ispell-help-list)) ?
  1250.     '(menu-item "Help"
  1251.             (lambda () (interactive) (describe-function 'ispell-help))
  1252.             :help "Show standard Ispell keybindings and commands"))
  1253.       (define-key ispell-menu-map [flyspell-mode]
  1254.     '(menu-item "Automatic spell checking (Flyspell)"
  1255.             flyspell-mode
  1256.             :help "Check spelling while you edit the text"
  1257.             :button (:toggle . (bound-and-true-p flyspell-mode))))
  1258.       (define-key ispell-menu-map [ispell-complete-word]
  1259.     '(menu-item "Complete Word" ispell-complete-word
  1260.             :help "Complete word at cursor using dictionary"))
  1261.       (define-key ispell-menu-map [ispell-complete-word-interior-frag]
  1262.     '(menu-item "Complete Word Fragment" ispell-complete-word-interior-frag
  1263.             :help "Complete word fragment at cursor"))))
  1264.  
  1265. ;;;###autoload
  1266. (if ispell-menu-map-needed
  1267.     (progn
  1268.       (define-key ispell-menu-map [ispell-continue]
  1269.     '(menu-item "Continue Spell-Checking" ispell-continue
  1270.             :enable (and (boundp 'ispell-region-end)
  1271.                  (marker-position ispell-region-end)
  1272.                  (equal (marker-buffer ispell-region-end)
  1273.                     (current-buffer)))
  1274.             :help "Continue spell checking last region"))
  1275.       (define-key ispell-menu-map [ispell-word]
  1276.     '(menu-item "Spell-Check Word" ispell-word
  1277.             :help "Spell-check word at cursor"))
  1278.       (define-key ispell-menu-map [ispell-comments-and-strings]
  1279.     '(menu-item "Spell-Check Comments" ispell-comments-and-strings
  1280.             :help "Spell-check only comments and strings"))))
  1281.  
  1282. ;;;###autoload
  1283. (if ispell-menu-map-needed
  1284.     (progn
  1285.       (define-key ispell-menu-map [ispell-region]
  1286.     '(menu-item "Spell-Check Region" ispell-region
  1287.             :enable mark-active
  1288.             :help "Spell-check text in marked region"))
  1289.       (define-key ispell-menu-map [ispell-message]
  1290.     '(menu-item "Spell-Check Message" ispell-message
  1291.             :visible (eq major-mode 'mail-mode)
  1292.             :help "Skip headers and included message text"))
  1293.       (define-key ispell-menu-map [ispell-buffer]
  1294.     '(menu-item "Spell-Check Buffer" ispell-buffer
  1295.             :help "Check spelling of selected buffer"))
  1296.       ;;(put 'ispell-region 'menu-enable 'mark-active)
  1297.       (fset 'ispell-menu-map (symbol-value 'ispell-menu-map))))
  1298.  
  1299. ;;; XEmacs versions 19 & 20
  1300. (if (and (featurep 'xemacs)
  1301.      (featurep 'menubar)
  1302.      ;;(null ispell-menu-xemacs)
  1303.      (not (and (boundp 'infodock-version) infodock-version)))
  1304.     (let ((dicts (if (fboundp 'ispell-valid-dictionary-list)
  1305.              (reverse (ispell-valid-dictionary-list))))
  1306.       (current-menubar (or current-menubar default-menubar))
  1307.       (menu
  1308.        '(["Help"        (describe-function 'ispell-help) t]
  1309.          ;;["Help"        (popup-menu ispell-help-list)    t]
  1310.          ["Check Message"    ispell-message            t]
  1311.          ["Check Buffer"    ispell-buffer            t]
  1312.          ["Check Comments"    ispell-comments-and-strings    t]
  1313.          ["Check Word"    ispell-word            t]
  1314.          ["Check Region"    ispell-region  (or (not zmacs-regions) (mark))]
  1315.          ["Continue Check"    ispell-continue            t]
  1316.          ["Complete Word Frag"ispell-complete-word-interior-frag t]
  1317.          ["Complete Word"    ispell-complete-word        t]
  1318.          ["Kill Process"    ispell-kill-ispell        t]
  1319.          ["Customize..."    (customize-group 'ispell)    t]
  1320.          ;; flyspell-mode may not be bound...
  1321.          ;;["flyspell"    flyspell-mode
  1322.          ;;            :style toggle :selected flyspell-mode ]
  1323.          "-"
  1324.          ["Save Personal Dict"(ispell-pdict-save t t)    t]
  1325.          ["Change Dictionary" ispell-change-dictionary    t])))
  1326.       (if (null dicts)
  1327.       (setq dicts (cons "default" nil)))
  1328.       (dolist (name dicts)
  1329.     (setq menu (append menu
  1330.                (list
  1331.                  (vector
  1332.                   (concat "Select " (capitalize name))
  1333.                   (list 'ispell-change-dictionary name)
  1334.                   t)))))
  1335.       (setq ispell-menu-xemacs menu)
  1336.       (if current-menubar
  1337.       (progn
  1338.         (if (car (find-menu-item current-menubar '("Cmds")))
  1339.         (progn
  1340.           ;; XEmacs 21.2
  1341.           (delete-menu-item '("Cmds" "Spell-Check"))
  1342.           (add-menu '("Cmds") "Spell-Check" ispell-menu-xemacs))
  1343.           ;; previous
  1344.           (delete-menu-item '("Edit" "Spell")) ; in case already defined
  1345.           (add-menu '("Edit") "Spell" ispell-menu-xemacs))))))
  1346.  
  1347. (defalias 'ispell-int-char
  1348.   ;; Allow incrementing characters as integers in XEmacs 20
  1349.   (if (and (featurep 'xemacs)
  1350.        (fboundp 'int-char))
  1351.       'int-char
  1352.     ;; Emacs and XEmacs 19 or earlier
  1353.     'identity))
  1354.  
  1355.  
  1356. ;;; **********************************************************************
  1357.  
  1358.  
  1359. ;;; This variable contains the current dictionary being used if the ispell
  1360. ;;; process is running.
  1361. (defvar ispell-current-dictionary nil
  1362.   "The name of the current dictionary, or nil for the default.
  1363. This is passed to the ispell process using the `-d' switch and is
  1364. used as key in `ispell-local-dictionary-alist' and `ispell-dictionary-alist'.")
  1365.  
  1366. (defvar ispell-current-personal-dictionary nil
  1367.   "The name of the current personal dictionary, or nil for the default.
  1368. This is passed to the ispell process using the `-p' switch.")
  1369.  
  1370. (defvar ispell-dictionary nil
  1371.   "Default dictionary to use if `ispell-local-dictionary' is nil.")
  1372.  
  1373. (defun ispell-decode-string (str)
  1374.   "Decodes multibyte character strings.
  1375. Protects against bogus binding of `enable-multibyte-characters' in XEmacs."
  1376.   ;; FIXME: enable-multibyte-characters is read-only, so bogus bindings are
  1377.   ;; really nasty (they signal an error in Emacs): Who does that?  --Stef
  1378.   (if (and (or (featurep 'xemacs)
  1379.            (and (boundp 'enable-multibyte-characters)
  1380.             enable-multibyte-characters))
  1381.        (fboundp 'decode-coding-string)
  1382.        (ispell-get-coding-system))
  1383.       (decode-coding-string str (ispell-get-coding-system))
  1384.     str))
  1385.  
  1386. ;; ----------- Debian changes ------
  1387.  
  1388. (setq debian-ispell-emacs-needs-unified-chars-mapping
  1389.       (boundp 'ucs-mule-8859-to-mule-unicode))
  1390.  
  1391. (setq debian-xemacs-last-coding-message nil)
  1392.  
  1393. (defun debian-ispell-get-coding-system ()
  1394.   (let (ispell-dict-encoding
  1395.     emacs-buffer-encoding
  1396.     tmp-debug-string)
  1397.     (setq ispell-dict-encoding
  1398.       (nth 7 (or (assoc ispell-current-dictionary ispell-local-dictionary-alist)
  1399.              (assoc ispell-current-dictionary ispell-dictionary-alist))))
  1400.  
  1401.     ;; (message "ispell-current-dictionary: %s, dict-coding:%s"
  1402.     ;; ispell-current-dictionary ispell-dict-encoding)
  1403.  
  1404.     (if debian-ispell-emacs-needs-unified-chars-mapping
  1405.     ispell-dict-encoding
  1406.       ;; No charsets unification
  1407.       (setq emacs-buffer-encoding (ispell-get-mime-charset))
  1408.       (when (and (boundp 'debian-dict-common-debug)
  1409.          debian-dict-common-debug)
  1410.     (setq tmp-debug-string
  1411.           (format "- codings: bfcs: %s, ecs: %s, ics: %s"
  1412.               (if (boundp 'buffer-file-coding-system)
  1413.               buffer-file-coding-system
  1414.             "unbound")
  1415.               emacs-buffer-encoding
  1416.               ispell-dict-encoding))
  1417.     (unless (string-equal tmp-debug-string debian-xemacs-last-coding-message)
  1418.       (message "%s" tmp-debug-string)
  1419.       (setq debian-xemacs-last-coding-message tmp-debug-string)))
  1420.  
  1421.       (if (and (string-equal emacs-buffer-encoding "iso-8859-15")
  1422.            (string-equal ispell-dict-encoding "iso-8859-1"))
  1423.       emacs-buffer-encoding
  1424.     ispell-dict-encoding))))
  1425.  
  1426. (when debian-ispell-emacs-needs-unified-chars-mapping
  1427.   (put 'ispell-unified-chars-table 'char-table-extra-slots 0)
  1428.  
  1429.   ;; Char-table that maps an Unicode character (charset:
  1430.   ;; latin-iso8859-1, mule-unicode-0100-24ff, mule-unicode-2500-34ff) to
  1431.   ;; a string in which all equivalent characters are listed.
  1432.  
  1433.   (defconst ispell-unified-chars-table
  1434.     (let ((table (make-char-table 'ispell-unified-chars-table)))
  1435.       (map-char-table
  1436.        #'(lambda (c v)
  1437.        (if (and v (/= c v))
  1438.            (let ((unified (or (aref table v) (string v))))
  1439.          (aset table v (concat unified (string c))))))
  1440.        ucs-mule-8859-to-mule-unicode)
  1441.       table))
  1442.   )
  1443.  
  1444. ;; Return a string decoded from Nth element of the current dictionary
  1445. ;; ---
  1446. ;; * If we are in unicode emacs, just make sure chars are made
  1447. ;;   multibyte if they are unibyte
  1448. ;; * If we are not in unicode emacs, and so ucs-mule-8859-to-mule-unicode
  1449. ;;   is defined, return the string while splicing equivalent characters
  1450. ;;   into the string.  This splicing is done only if the string is a
  1451. ;;   regular expression of the form "[...]" because, otherwise, splicing
  1452. ;;   will result in incorrect regular expression matching.
  1453. ;; * If we are in xemacs none of the above applies, just call ispell-decode-string
  1454.  
  1455. (defun ispell-get-decoded-string (n)
  1456.   (let* ((slot (or
  1457.         (assoc ispell-current-dictionary ispell-local-dictionary-alist)
  1458.         (assoc ispell-current-dictionary ispell-dictionary-alist)))
  1459.      (str (nth n slot)))
  1460.  
  1461.     (if (and (< emacs-major-version 22)
  1462.          (not (boundp 'ucs-mule-8859-to-mule-unicode)))
  1463.     ;; xemacs21 or ancient emacs21 < 21.3
  1464.     (setq str (ispell-decode-string str))
  1465.       (when (and (> (length str) 0)
  1466.          (not (multibyte-string-p str)))
  1467.     (setq str (ispell-decode-string str))
  1468.     (if (not debian-ispell-emacs-needs-unified-chars-mapping)
  1469.         ;; unicode branch. We already know this is unibyte (see test above)
  1470.         (setq str (string-to-multibyte str))
  1471.       ;; No unicode branch, but not so ancient code (emacs21 > 21.3 or emacs22). Do the mapping
  1472.       (if (and (= (aref str 0) ?\[)
  1473.            (eq (string-match "\\]" str) (1- (length str))))
  1474.           (setq str
  1475.             (string-as-multibyte
  1476.              (mapconcat
  1477.               #'(lambda (c)
  1478.               (let ((unichar (aref ucs-mule-8859-to-mule-unicode c)))
  1479.                 (if unichar
  1480.                 (aref ispell-unified-chars-table unichar)
  1481.                   (string c))))
  1482.               str "")))))))
  1483.     str))
  1484.  
  1485. ;; ----------- End of Debian changes ------
  1486.  
  1487. (defun ispell-get-casechars ()
  1488.   (ispell-get-decoded-string 1))
  1489. (defun ispell-get-not-casechars ()
  1490.   (ispell-get-decoded-string 2))
  1491. (defun ispell-get-otherchars ()
  1492.   (ispell-get-decoded-string 3))
  1493. (defun ispell-get-many-otherchars-p ()
  1494.   (nth 4 (or (assoc ispell-current-dictionary ispell-local-dictionary-alist)
  1495.          (assoc ispell-current-dictionary ispell-dictionary-alist))))
  1496. (defun ispell-get-ispell-args ()
  1497.   (nth 5 (or (assoc ispell-current-dictionary ispell-local-dictionary-alist)
  1498.          (assoc ispell-current-dictionary ispell-dictionary-alist))))
  1499. (defun ispell-get-extended-character-mode ()
  1500.   (nth 6 (or (assoc ispell-current-dictionary ispell-local-dictionary-alist)
  1501.          (assoc ispell-current-dictionary ispell-dictionary-alist))))
  1502. (defalias 'ispell-get-coding-system 'debian-ispell-get-coding-system)
  1503.  
  1504.  
  1505. (defvar ispell-pdict-modified-p nil
  1506.   "Non-nil means personal dictionary has modifications to be saved.")
  1507.  
  1508. ;;; If you want to save the dictionary when quitting, must do so explicitly.
  1509. ;;; When non-nil, the spell session is terminated.
  1510. ;;; When numeric, contains cursor location in buffer, and cursor remains there.
  1511. (defvar ispell-quit nil)
  1512.  
  1513. (defvar ispell-process-directory nil
  1514.   "The directory where `ispell-process' was started.")
  1515.  
  1516. (defvar ispell-filter nil
  1517.   "Output filter from piped calls to Ispell.")
  1518.  
  1519. (defvar ispell-filter-continue nil
  1520.   "Control variable for Ispell filter function.")
  1521.  
  1522. (defvar ispell-output-buffer nil
  1523.   "Buffer used for reading output of a synchronous Ispell subprocess.")
  1524.  
  1525. (defvar ispell-session-buffer nil
  1526.   "Buffer used for passing input to a synchronous Ispell subprocess.")
  1527.  
  1528. (defvar ispell-cmd-args nil
  1529.   "Command-line arguments to pass to a synchronous Ispell subprocess.")
  1530.  
  1531. (defvar ispell-query-replace-marker (make-marker)
  1532.   "Marker for `query-replace' processing.")
  1533.  
  1534. (defvar ispell-recursive-edit-marker (make-marker)
  1535.   "Marker for return point from recursive edit.")
  1536.  
  1537. (defvar ispell-checking-message nil
  1538.   "Non-nil when we're checking a mail message.
  1539. Set to the MIME boundary locations when checking messages.")
  1540.  
  1541. (defconst ispell-choices-buffer "*Choices*")
  1542.  
  1543. (defvar ispell-overlay nil "Overlay variable for Ispell highlighting.")
  1544.  
  1545. ;;; *** Buffer Local Definitions ***
  1546.  
  1547. (defconst ispell-words-keyword "LocalWords: "
  1548.   "The keyword for local oddly-spelled words to accept.
  1549. The keyword will be followed by any number of local word spellings.
  1550. There can be multiple of these keywords in the file.")
  1551.  
  1552. (defconst ispell-dictionary-keyword "Local IspellDict: "
  1553.   "The keyword for a local dictionary to use.
  1554. The keyword must be followed by a valid dictionary name, defined in
  1555. `ispell-local-dictionary-alist' or `ispell-dictionary-alist'.
  1556. When multiple occurrences exist, the last keyword
  1557. definition is used.")
  1558.  
  1559. (defconst ispell-pdict-keyword "Local IspellPersDict: "
  1560.   "The keyword for defining buffer local dictionaries.
  1561. Keyword must be followed by the filename of a personal dictionary.
  1562. The last occurring definition in the buffer will be used.")
  1563.  
  1564. (defconst ispell-parsing-keyword "Local IspellParsing: "
  1565.   "The keyword for overriding default Ispell parsing.
  1566. The above keyword string should be followed by `latex-mode' or
  1567. `nroff-mode' to put the current buffer into the desired parsing mode.
  1568.  
  1569. Extended character mode can be changed for this buffer by placing
  1570. a `~' followed by an extended-character mode -- such as `~.tex'.
  1571. The last occurring definition in the buffer will be used.")
  1572.  
  1573. ;;;###autoload
  1574. (defvar ispell-skip-region-alist
  1575.   '((ispell-words-keyword       forward-line)
  1576.     (ispell-dictionary-keyword       forward-line)
  1577.     (ispell-pdict-keyword       forward-line)
  1578.     (ispell-parsing-keyword       forward-line)
  1579.     ("^---*BEGIN PGP [A-Z ]*--*" . "^---*END PGP [A-Z ]*--*")
  1580.     ;; assume multiline uuencoded file? "\nM.*$"?
  1581.     ("^begin [0-9][0-9][0-9] [^ \t]+$" . "\nend\n")
  1582.     ("^%!PS-Adobe-[123].0"     . "\n%%EOF\n")
  1583.     ("^---* \\(Start of \\)?[Ff]orwarded [Mm]essage"
  1584.      . "^---* End of [Ff]orwarded [Mm]essage")
  1585.     ;; Matches e-mail addresses, file names, http addresses, etc.  The
  1586.     ;; `-+' `_+' patterns are necessary for performance reasons when
  1587.     ;; `-' or `_' part of word syntax.
  1588.     ("\\(--+\\|_+\\|\\(/\\w\\|\\(\\(\\w\\|[-_]\\)+[.:@]\\)\\)\\(\\w\\|[-_]\\)*\\([.:/@]+\\(\\w\\|[-_~=?&]\\)+\\)+\\)")
  1589.     ;; above checks /.\w sequences
  1590.     ;;("\\(--+\\|\\(/\\|\\(\\(\\w\\|[-_]\\)+[.:@]\\)\\)\\(\\w\\|[-_]\\)*\\([.:/@]+\\(\\w\\|[-_~=?&]\\)+\\)+\\)")
  1591.     ;; This is a pretty complex regexp.  It can be simplified to the following:
  1592.     ;; "\\(\\w\\|[-_]\\)*\\([.:/@]+\\(\\w\\|[-_]\\|~\\)+\\)+"
  1593.     ;; but some valid text will be skipped, e.g. "his/her".  This could be
  1594.     ;; fixed up (at the expense of a moderately more complex regexp)
  1595.     ;; by not allowing "/" to be the character which triggers the
  1596.     ;; identification of the computer name, e.g.:
  1597.     ;; "\\(\\w\\|[-_]\\)+[.:@]\\(\\w\\|[-_]\\)*\\([.:/@]+\\(\\w\\|[-_]\\|~\\)+\\)+"
  1598.     )
  1599.   "Alist expressing beginning and end of regions not to spell check.
  1600. The alist key must be a regular expression.
  1601. Valid forms include:
  1602.   (KEY) - just skip the key.
  1603.   (KEY . REGEXP) - skip to the end of REGEXP.  REGEXP may be string or symbol.
  1604.   (KEY REGEXP) - skip to end of REGEXP.  REGEXP must be a string.
  1605.   (KEY FUNCTION ARGS) - FUNCTION called with ARGS returns end of region.")
  1606. (put 'ispell-skip-region-alist 'risky-local-variable t)
  1607.  
  1608.  
  1609. ;;;###autoload
  1610. (defvar ispell-tex-skip-alists
  1611.   '((;;("%\\[" . "%\\]") ; AMStex block comment...
  1612.      ;; All the standard LaTeX keywords from L. Lamport's guide:
  1613.      ;; \cite, \hspace, \hspace*, \hyphenation, \include, \includeonly, \input,
  1614.      ;; \label, \nocite, \rule (in ispell - rest included here)
  1615.      ("\\\\addcontentsline"              ispell-tex-arg-end 2)
  1616.      ("\\\\add\\(tocontents\\|vspace\\)" ispell-tex-arg-end)
  1617.      ("\\\\\\([aA]lph\\|arabic\\)"     ispell-tex-arg-end)
  1618.      ;;("\\\\author"             ispell-tex-arg-end)
  1619.      ("\\\\bibliographystyle"         ispell-tex-arg-end)
  1620.      ("\\\\makebox"             ispell-tex-arg-end 0)
  1621.      ("\\\\e?psfig"             ispell-tex-arg-end)
  1622.      ("\\\\document\\(class\\|style\\)" .
  1623.       "\\\\begin[ \t\n]*{[ \t\n]*document[ \t\n]*}"))
  1624.     (;; delimited with \begin.  In ispell: displaymath, eqnarray, eqnarray*,
  1625.      ;; equation, minipage, picture, tabular, tabular* (ispell)
  1626.      ("\\(figure\\|table\\)\\*?"     ispell-tex-arg-end 0)
  1627.      ("list"                 ispell-tex-arg-end 2)
  1628.      ("program"        . "\\\\end[ \t\n]*{[ \t\n]*program[ \t\n]*}")
  1629.      ("verbatim\\*?"    . "\\\\end[ \t\n]*{[ \t\n]*verbatim\\*?[ \t\n]*}")))
  1630.   "*Lists of regions to be skipped in TeX mode.
  1631. First list is used raw.
  1632. Second list has key placed inside \\begin{}.
  1633.  
  1634. Delete or add any regions you want to be automatically selected
  1635. for skipping in latex mode.")
  1636. (put 'ispell-tex-skip-alist 'risky-local-variable t)
  1637.  
  1638.  
  1639. ;;;###autoload
  1640. (defvar ispell-html-skip-alists
  1641.   '(("<[cC][oO][dD][eE]\\>[^>]*>"      "</[cC][oO][dD][eE]*>")
  1642.     ("<[sS][cC][rR][iI][pP][tT]\\>[^>]*>" "</[sS][cC][rR][iI][pP][tT]>")
  1643.     ("<[aA][pP][pP][lL][eE][tT]\\>[^>]*>" "</[aA][pP][pP][lL][eE][tT]>")
  1644.     ("<[vV][eE][rR][bB]\\>[^>]*>"         "<[vV][eE][rR][bB]\\>[^>]*>")
  1645.     ;;("<[tT][tT]\\>[^>]*>"          "<[tT][tT]\\>[^>]*>")
  1646.     ("<[tT][tT]/"              "/")
  1647.     ("<[^ \t\n>]"              ">")
  1648.     ("&[^ \t\n;]"              "[; \t\n]"))
  1649.   "*Lists of start and end keys to skip in HTML buffers.
  1650. Same format as `ispell-skip-region-alist'.
  1651. Note - substrings of other matches must come last
  1652.  (e.g. \"<[tT][tT]/\" and \"<[^ \\t\\n>]\").")
  1653. (put 'ispell-html-skip-alists 'risky-local-variable t)
  1654.  
  1655. (defvar ispell-local-pdict ispell-personal-dictionary
  1656.   "A buffer local variable containing the current personal dictionary.
  1657. If non-nil, the value must be a string, which is a file name.
  1658.  
  1659. If you specify a personal dictionary for the current buffer which is
  1660. different from the current personal dictionary, the effect is similar
  1661. to calling \\[ispell-change-dictionary].  This variable is automatically
  1662. set when defined in the file with either `ispell-pdict-keyword' or the
  1663. local variable syntax.")
  1664.  
  1665. (make-variable-buffer-local 'ispell-local-pdict)
  1666. ;;;###autoload(put 'ispell-local-pdict 'safe-local-variable 'stringp)
  1667.  
  1668. (defvar ispell-buffer-local-name nil
  1669.   "Contains the buffer name if local word definitions were used.
  1670. Ispell is then restarted because the local words could conflict.")
  1671.  
  1672. (defvar ispell-parser 'use-mode-name
  1673.    "*Indicates whether ispell should parse the current buffer as TeX Code.
  1674. Special value `use-mode-name' tries to guess using the name of `major-mode'.
  1675. Default parser is `nroff'.
  1676. Currently the only other valid parser is `tex'.
  1677.  
  1678. You can set this variable in hooks in your init file -- eg:
  1679.  
  1680. \(add-hook 'tex-mode-hook (lambda () (setq ispell-parser 'tex)))")
  1681.  
  1682. (defvar ispell-region-end (make-marker)
  1683.   "Marker that allows spelling continuations.")
  1684.  
  1685. (defvar ispell-check-only nil
  1686.   "If non-nil, `ispell-word' does not try to correct the word.")
  1687.  
  1688.  
  1689. ;;; **********************************************************************
  1690. ;;; **********************************************************************
  1691.  
  1692.  
  1693.  
  1694. ;;;###autoload (define-key esc-map "$" 'ispell-word)
  1695.  
  1696.  
  1697. (defun ispell-accept-output (&optional timeout-secs timeout-msecs)
  1698.   "Wait for output from ispell process, or TIMEOUT-SECS and TIMEOUT-MSECS.
  1699. If asynchronous subprocesses are not supported, call `ispell-filter' and
  1700. pass it the output of the last ispell invocation."
  1701.   (if ispell-async-processp
  1702.       (accept-process-output ispell-process timeout-secs timeout-msecs)
  1703.     (if (null ispell-process)
  1704.     (error "No Ispell process to read output from!")
  1705.       (let ((buf ispell-output-buffer)
  1706.         ispell-output)
  1707.     (if (not (bufferp buf))
  1708.         (setq ispell-filter nil)
  1709.       (save-excursion
  1710.         (set-buffer buf)
  1711.         (setq ispell-output (buffer-substring-no-properties
  1712.                  (point-min) (point-max))))
  1713.       (ispell-filter t ispell-output)
  1714.       (save-excursion
  1715.         (set-buffer buf)
  1716.         (erase-buffer)))))))
  1717.  
  1718. (defun ispell-send-replacement (misspelled replacement)
  1719.   "Notify aspell that MISSPELLED should be spelled REPLACEMENT.
  1720. This allows it to improve the suggestion list based on actual misspellings."
  1721.   (and ispell-really-aspell
  1722.        (ispell-send-string (concat "$$ra " misspelled "," replacement "\n"))))
  1723.  
  1724.  
  1725. (defun ispell-send-string (string)
  1726.   "Send the string STRING to the Ispell process."
  1727.   (if ispell-async-processp
  1728.       (process-send-string ispell-process string)
  1729.     ;; Asynchronous subprocesses aren't supported on this losing system.
  1730.     ;; We keep all the directives passed to Ispell during the entire
  1731.     ;; session in a buffer, and pass them anew each time we invoke
  1732.     ;; Ispell to process another chunk of text.  (Yes, I know this is a
  1733.     ;; terrible kludge, and it's a bit slow, but it does get the work done.)
  1734.     (let ((cmd (aref string 0))
  1735.       ;; The following commands are not passed to Ispell until
  1736.       ;; we have a *real* reason to invoke it.
  1737.       (cmds-to-defer '(?* ?@ ?~ ?+ ?- ?! ?%))
  1738.       (default-major-mode 'fundamental-mode)
  1739.       (session-buf ispell-session-buffer)
  1740.       (output-buf ispell-output-buffer)
  1741.       (ispell-args ispell-cmd-args)
  1742.       (defdir ispell-process-directory)
  1743.       prev-pos)
  1744.       (save-excursion
  1745.     (set-buffer session-buf)
  1746.     (setq prev-pos (point))
  1747.     (setq default-directory defdir)
  1748.     (insert string)
  1749.     (if (not (memq cmd cmds-to-defer))
  1750.         (let (coding-system-for-read coding-system-for-write status)
  1751.           (if (and (boundp 'enable-multibyte-characters)
  1752.                enable-multibyte-characters)
  1753.           (setq coding-system-for-read (ispell-get-coding-system)
  1754.             coding-system-for-write (ispell-get-coding-system)))
  1755.           (set-buffer output-buf)
  1756.           (erase-buffer)
  1757.           (set-buffer session-buf)
  1758.           (setq status
  1759.             (apply 'ispell-call-process-region
  1760.                (point-min) (point-max)
  1761.                ispell-program-name nil
  1762.                output-buf nil
  1763.                "-a"
  1764.                ;; hunspell -m option means something different
  1765.                (if ispell-really-hunspell "" "-m")
  1766.                ispell-args))
  1767.           (set-buffer output-buf)
  1768.           (goto-char (point-min))
  1769.           (save-match-data
  1770.         (if (not (looking-at "@(#) "))
  1771.             (error "Ispell error: %s"
  1772.                (buffer-substring-no-properties
  1773.                 (point) (progn (end-of-line) (point)))))
  1774.         ;; If STRING is "^Z\n", we just started Ispell and need
  1775.         ;; to retain its version ID line in the output buffer.
  1776.         ;; Otherwise, remove the ID line, as it will confuse
  1777.         ;; `ispell-filter'.
  1778.         (or (string= string "\032\n")
  1779.             (progn
  1780.               (forward-line)
  1781.               (delete-region (point-min) (point))))
  1782.         ;; If STRING begins with ^ or any normal character, we need
  1783.         ;; to remove the last line from the session buffer, since it
  1784.         ;; was just spell-checked, and we don't want to check it again.
  1785.         ;; The same goes for the # command, since Ispell already saved
  1786.         ;; the personal dictionary.
  1787.         (set-buffer session-buf)
  1788.         (delete-region prev-pos (point))
  1789.         ;; Ispell run synchronously saves the personal dictionary
  1790.         ;; after each successful command.  So we can remove any
  1791.         ;; lines in the session buffer that insert words into the
  1792.         ;; dictionary.
  1793.         (if (memq status '(0 nil))
  1794.             (let ((more-lines t))
  1795.               (goto-char (point-min))
  1796.               (while more-lines
  1797.             (if (looking-at "^\\*")
  1798.                 (let ((start (point)))
  1799.                   (forward-line)
  1800.                   (delete-region start (point)))
  1801.               (setq more-lines (= 0 (forward-line))))))))))))))
  1802.  
  1803.  
  1804. ;; Insert WORD while translating Latin characters to the equivalent
  1805. ;; characters that is supported by buffer-file-coding-system.
  1806.  
  1807. (defun ispell-insert-word (word)
  1808.   (let ((pos (point)))
  1809.     (insert word)
  1810.     (if (and (boundp 'translation-table-for-input) ;; xemacs does not have this
  1811.          (> emacs-major-version 21)            ;; and emacs21 expects a string
  1812.          (char-table-p translation-table-for-input))
  1813.     (translate-region pos (point) translation-table-for-input))))
  1814.  
  1815. ;;;###autoload
  1816. (defun ispell-word (&optional following quietly continue region)
  1817.   "Check spelling of word under or before the cursor.
  1818. If the word is not found in dictionary, display possible corrections
  1819. in a window allowing you to choose one.
  1820.  
  1821. If optional argument FOLLOWING is non-nil or if `ispell-following-word'
  1822. is non-nil when called interactively, then the following word
  1823. \(rather than preceding\) is checked when the cursor is not over a word.
  1824. When the optional argument QUIETLY is non-nil or `ispell-quietly' is non-nil
  1825. when called interactively, non-corrective messages are suppressed.
  1826.  
  1827. With a prefix argument (or if CONTINUE is non-nil),
  1828. resume interrupted spell-checking of a buffer or region.
  1829.  
  1830. Interactively, in Transient Mark mode when the mark is active, call
  1831. `ispell-region' to check the active region for spelling errors.
  1832.  
  1833. Word syntax is controlled by the definition of the chosen dictionary,
  1834. which is in `ispell-local-dictionary-alist' or `ispell-dictionary-alist'.
  1835.  
  1836. This will check or reload the dictionary.  Use \\[ispell-change-dictionary]
  1837. or \\[ispell-region] to update the Ispell process.
  1838.  
  1839. Return values:
  1840. nil           word is correct or spelling is accepted.
  1841. 0             word is inserted into buffer-local definitions.
  1842. \"word\"        word corrected from word list.
  1843. \(\"word\" arg\)  word is hand entered.
  1844. quit          spell session exited."
  1845.   (interactive (list ispell-following-word ispell-quietly current-prefix-arg t))
  1846.   (cond
  1847.    ((and region
  1848.      (if (fboundp 'use-region-p)
  1849.          (use-region-p)
  1850.        (and (boundp 'transient-mark-mode) transient-mark-mode
  1851.         (boundp 'mark-active) mark-active
  1852.         (not (eq (region-beginning) (region-end))))))
  1853.     (ispell-region (region-beginning) (region-end)))
  1854.    (continue (ispell-continue))
  1855.    (t
  1856.     (ispell-set-spellchecker-params)    ; Initialize variables and dicts alists
  1857.     (ispell-accept-buffer-local-defs)    ; use the correct dictionary
  1858.     (let ((cursor-location (point))    ; retain cursor location
  1859.       (word (ispell-get-word following))
  1860.       start end poss new-word replace)
  1861.       ;; De-structure return word info list.
  1862.       (setq start (car (cdr word))
  1863.         end (car (cdr (cdr word)))
  1864.         word (car word))
  1865.  
  1866.       ;; At this point it used to ignore 2-letter words.
  1867.       ;; But that is silly; if the user asks for it, we should do it. - rms.
  1868.       (or quietly
  1869.       (message "Checking spelling of %s..."
  1870.            (funcall ispell-format-word-function word)))
  1871.       (ispell-send-string "%\n")    ; put in verbose mode
  1872.       (ispell-send-string (concat "^" word "\n"))
  1873.       ;; wait until ispell has processed word
  1874.       (while (progn
  1875.            (ispell-accept-output)
  1876.            (not (string= "" (car ispell-filter)))))
  1877.       ;;(ispell-send-string "!\n") ;back to terse mode.
  1878.       (setq ispell-filter (cdr ispell-filter)) ; remove extra \n
  1879.       (if (and ispell-filter (listp ispell-filter))
  1880.       (if (> (length ispell-filter) 1)
  1881.           (error "Ispell and its process have different character maps")
  1882.         (setq poss (ispell-parse-output (car ispell-filter)))))
  1883.       (cond ((eq poss t)
  1884.          (or quietly
  1885.          (message "%s is correct"
  1886.               (funcall ispell-format-word-function word)))
  1887.          (and (featurep 'xemacs)
  1888.           (extent-at start)
  1889.           (and (fboundp 'delete-extent)
  1890.                (delete-extent (extent-at start)))))
  1891.         ((stringp poss)
  1892.          (or quietly
  1893.          (message "%s is correct because of root %s"
  1894.               (funcall ispell-format-word-function word)
  1895.               (funcall ispell-format-word-function poss)))
  1896.          (and (featurep 'xemacs)
  1897.           (extent-at start)
  1898.           (and (fboundp 'delete-extent)
  1899.                (delete-extent (extent-at start)))))
  1900.         ((null poss) (message "Error in ispell process"))
  1901.         (ispell-check-only          ; called from ispell minor mode.
  1902.          (if (fboundp 'make-extent)
  1903.          (if (fboundp 'set-extent-property)
  1904.              (let ((ext (make-extent start end)))
  1905.                (set-extent-property ext 'face ispell-highlight-face)
  1906.                (set-extent-property ext 'priority 2000)))
  1907.            (beep)
  1908.            (message "%s is incorrect"
  1909.                         (funcall ispell-format-word-function word))))
  1910.         (t                ; prompt for correct word.
  1911.          (save-window-excursion
  1912.            (setq replace (ispell-command-loop
  1913.                   (car (cdr (cdr poss)))
  1914.                   (car (cdr (cdr (cdr poss))))
  1915.                   (car poss) start end)))
  1916.          (cond ((equal 0 replace)
  1917.             (ispell-add-per-file-word-list (car poss)))
  1918.            (replace
  1919.             (setq new-word (if (atom replace) replace (car replace))
  1920.               cursor-location (+ (- (length word) (- end start))
  1921.                          cursor-location))
  1922.             (if (not (equal new-word (car poss)))
  1923.             (progn
  1924.               (goto-char start)
  1925.               ;; Insert first and then delete,
  1926.               ;; to avoid collapsing markers before and after
  1927.               ;; into a single place.
  1928.               (ispell-insert-word new-word)
  1929.               (delete-region (point) end)
  1930.               ;; It is meaningless to preserve the cursor position
  1931.               ;; inside a word that has changed.
  1932.               (setq cursor-location (point))
  1933.               (setq end (point))))
  1934.             (if (not (atom replace)) ;recheck spelling of replacement
  1935.             (progn
  1936.               (if (car (cdr replace)) ; query replace requested
  1937.                   (save-window-excursion
  1938.                 (query-replace word new-word t)))
  1939.               (goto-char start)
  1940.               ;; single word could be split into multiple words
  1941.               (setq ispell-quit (not (ispell-region start end)))
  1942.               ))))
  1943.          ;; keep if rechecking word and we keep choices win.
  1944.          (if (get-buffer ispell-choices-buffer)
  1945.          (kill-buffer ispell-choices-buffer))))
  1946.       (ispell-pdict-save ispell-silently-savep)
  1947.       ;; NB: Cancels ispell-quit incorrectly if called from ispell-region
  1948.       (if ispell-quit (setq ispell-quit nil replace 'quit))
  1949.       (goto-char cursor-location)    ; return to original location
  1950.       replace))))
  1951.  
  1952.  
  1953. (defun ispell-get-word (following &optional extra-otherchars)
  1954.   "Return the word for spell-checking according to ispell syntax.
  1955. If optional argument FOLLOWING is non-nil or if `ispell-following-word'
  1956. is non-nil when called interactively, then the following word
  1957. \(rather than preceding\) is checked when the cursor is not over a word.
  1958. Optional second argument contains otherchars that can be included in word
  1959. many times.
  1960.  
  1961. Word syntax is controlled by the definition of the chosen dictionary,
  1962. which is in `ispell-local-dictionary-alist' or `ispell-dictionary-alist'."
  1963.   (let* ((ispell-casechars (ispell-get-casechars))
  1964.      (ispell-not-casechars (ispell-get-not-casechars))
  1965.      (ispell-otherchars (ispell-get-otherchars))
  1966.      (ispell-many-otherchars-p (ispell-get-many-otherchars-p))
  1967.      (word-regexp (concat ispell-casechars
  1968.                   "+\\("
  1969.                   (if (not (string= "" ispell-otherchars))
  1970.                   (concat ispell-otherchars "?"))
  1971.                   (if extra-otherchars
  1972.                   (concat extra-otherchars "?"))
  1973.                   ispell-casechars
  1974.                   "+\\)"
  1975.                   (if (or ispell-many-otherchars-p
  1976.                       extra-otherchars)
  1977.                   "*" "?")))
  1978.      did-it-once prevpt
  1979.      start end word)
  1980.     ;; find the word
  1981.     (if (not (looking-at ispell-casechars))
  1982.     (if following
  1983.         (re-search-forward ispell-casechars (point-max) t)
  1984.       (re-search-backward ispell-casechars (point-min) t)))
  1985.     ;; move to front of word
  1986.     (re-search-backward ispell-not-casechars (point-min) 'start)
  1987.     (while (and (or (and (not (string= "" ispell-otherchars))
  1988.              (looking-at ispell-otherchars))
  1989.             (and extra-otherchars (looking-at extra-otherchars)))
  1990.         (not (bobp))
  1991.         (or (not did-it-once)
  1992.             ispell-many-otherchars-p)
  1993.         (not (eq prevpt (point))))
  1994.       (if (and extra-otherchars (looking-at extra-otherchars))
  1995.       (progn
  1996.         (backward-char 1)
  1997.         (if (looking-at ispell-casechars)
  1998.         (re-search-backward ispell-not-casechars (point-min) 'move)))
  1999.     (setq did-it-once t
  2000.           prevpt (point))
  2001.     (backward-char 1)
  2002.     (if (looking-at ispell-casechars)
  2003.         (re-search-backward ispell-not-casechars (point-min) 'move)
  2004.       (backward-char -1))))
  2005.     ;; Now mark the word and save to string.
  2006.     (if (not (re-search-forward word-regexp (point-max) t))
  2007.     (if ispell-check-only
  2008.         ;; return dummy word when just flagging misspellings
  2009.         (list "" (point) (point))
  2010.       (error "No word found to check!"))
  2011.       (setq start (copy-marker (match-beginning 0))
  2012.         end (point-marker)
  2013.         word (buffer-substring-no-properties start end))
  2014.       (list word start end))))
  2015.  
  2016.  
  2017. ;;; Global ispell-pdict-modified-p is set by ispell-command-loop and
  2018. ;;; tracks changes in the dictionary.  The global may either be
  2019. ;;; a value or a list, whose value is the state of whether the
  2020. ;;; dictionary needs to be saved.
  2021.  
  2022. ;;;###autoload
  2023. (defun ispell-pdict-save (&optional no-query force-save)
  2024.   "Check to see if the personal dictionary has been modified.
  2025. If so, ask if it needs to be saved."
  2026.   (interactive (list ispell-silently-savep t))
  2027.   (if (and ispell-pdict-modified-p (listp ispell-pdict-modified-p))
  2028.       (setq ispell-pdict-modified-p (car ispell-pdict-modified-p)))
  2029.   (if (or ispell-pdict-modified-p force-save)
  2030.       (if (or no-query (y-or-n-p "Personal dictionary modified.  Save? "))
  2031.       (progn
  2032.         (ispell-send-string "#\n")    ; save dictionary
  2033.         (message "Personal dictionary saved."))))
  2034.   ;; unassert variable, even if not saved to avoid questioning.
  2035.   (setq ispell-pdict-modified-p nil))
  2036.  
  2037.  
  2038. (defun ispell-command-loop (miss guess word start end)
  2039.   "Display possible corrections from list MISS.
  2040. GUESS lists possibly valid affix construction of WORD.
  2041. Returns nil to keep word.
  2042. Returns 0 to insert locally into buffer-local dictionary.
  2043. Returns string for new chosen word.
  2044. Returns list for new replacement word (will be rechecked).
  2045.   Query-replace when list length is 2.
  2046.   Automatic query-replace when second element is `query-replace'.
  2047. Highlights the word, which is assumed to run from START to END.
  2048. Global `ispell-pdict-modified-p' becomes a list where the only value
  2049. indicates whether the dictionary has been modified when option `a'
  2050. or `i' is used.
  2051. Global `ispell-quit' set to start location to continue spell session."
  2052.   (let ((count ?0)
  2053.     (line ispell-choices-win-default-height)
  2054.     ;; ensure 4 context lines.
  2055.     (max-lines (- (ispell-adjusted-window-height) 4))
  2056.     (choices miss)
  2057.     (window-min-height (min window-min-height
  2058.                 ispell-choices-win-default-height))
  2059.     (command-characters '( ?  ?i ?a ?A ?r ?R ?? ?x ?X ?q ?l ?u ?m ))
  2060.     (dedicated (window-dedicated-p (selected-window)))
  2061.     (skipped 0)
  2062.     char num result textwin dedicated-win)
  2063.  
  2064.     ;; setup the *Choices* buffer with valid data.
  2065.     (save-excursion
  2066.       (set-buffer (get-buffer-create ispell-choices-buffer))
  2067.       (setq mode-line-format
  2068.         (concat "--  %b  --  word: " word
  2069.             "  --  dict: " (or ispell-current-dictionary "default")
  2070.             "  --  prog: " (file-name-nondirectory ispell-program-name)))
  2071.       ;; XEmacs: no need for horizontal scrollbar in choices window
  2072.       (ispell-with-no-warnings
  2073.        (and (fboundp 'set-specifier)
  2074.         (boundp 'horizontal-scrollbar-visible-p)
  2075.         (set-specifier horizontal-scrollbar-visible-p nil
  2076.                (cons (current-buffer) nil))))
  2077.       (erase-buffer)
  2078.       (if guess
  2079.       (progn
  2080.         (insert "Note: Affix rules generate and capitalize "
  2081.             "this word as shown below:\n\t")
  2082.         (while guess
  2083.           (if (> (+ 4 (current-column) (length (car guess)))
  2084.              (window-width))
  2085.           (progn
  2086.             (insert "\n\t")
  2087.             (setq line (1+ line))))
  2088.           (insert (car guess) "    ")
  2089.           (setq guess (cdr guess)))
  2090.         (insert "\nUse option `i' to accept this spelling and put it in your private dictionary.")
  2091.         (setq line (+ line (if choices 3 2)))))
  2092.       (while (and choices
  2093.           (< (if (> (+ 7 (current-column) (length (car choices))
  2094.                    (if (> count ?~) 3 0))
  2095.                 (window-width))
  2096.              (progn
  2097.                (insert "\n")
  2098.                (setq line (1+ line)))
  2099.                line)
  2100.              max-lines))
  2101.     ;; not so good if there are over 20 or 30 options, but then, if
  2102.     ;; there are that many you don't want to scan them all anyway...
  2103.     (while (memq count command-characters) ; skip command characters.
  2104.       (setq count (ispell-int-char (1+ count))
  2105.         skipped (1+ skipped)))
  2106.     (insert "(" count ") " (car choices) "  ")
  2107.     (setq choices (cdr choices)
  2108.           count (ispell-int-char (1+ count))))
  2109.       (setq count (ispell-int-char (- count ?0 skipped))))
  2110.  
  2111.     ;; ensure word is visible
  2112.     (if (not (pos-visible-in-window-p end))
  2113.     (sit-for 0))
  2114.  
  2115.     ;; allow temporary split of dedicated windows...
  2116.     (if dedicated
  2117.     (progn
  2118.       (setq dedicated-win (selected-window))
  2119.       (set-window-dedicated-p dedicated-win nil)))
  2120.  
  2121.     ;; Display choices for misspelled word.
  2122.     (ispell-show-choices line end)
  2123.     (select-window (setq textwin (next-window)))
  2124.  
  2125.     ;; highlight word, protecting current buffer status
  2126.     (unwind-protect
  2127.     (progn
  2128.       (and ispell-highlight-p
  2129.            (ispell-highlight-spelling-error start end t))
  2130.       ;; Loop until a valid choice is made.
  2131.       (while
  2132.           (eq
  2133.            t
  2134.            (setq
  2135.         result
  2136.         (progn
  2137.           (undo-boundary)
  2138.           (let (message-log-max)
  2139.             (message (concat "C-h or ? for more options; SPC to leave "
  2140.                      "unchanged, Character to replace word")))
  2141.           (let ((inhibit-quit t))
  2142.             (setq char (if (fboundp 'read-char-exclusive)
  2143.                    (read-char-exclusive)
  2144.                  (read-char))
  2145.               skipped 0)
  2146.             (if (or quit-flag (= char ?\C-g)) ; C-g is like typing X
  2147.             (setq char ?X
  2148.                   quit-flag nil)))
  2149.           ;; Adjust num to array offset skipping command characters.
  2150.           (let ((com-chars command-characters))
  2151.             (while com-chars
  2152.               (if (and (> (car com-chars) ?0) (< (car com-chars) char))
  2153.               (setq skipped (1+ skipped)))
  2154.               (setq com-chars (cdr com-chars)))
  2155.             (setq num (- char ?0 skipped)))
  2156.  
  2157.           (cond
  2158.            ((= char ? ) nil)    ; accept word this time only
  2159.            ((= char ?i)        ; accept and insert word into pers dict
  2160.             (ispell-send-string (concat "*" word "\n"))
  2161.             (setq ispell-pdict-modified-p '(t)) ; dictionary modified!
  2162.             nil)
  2163.            ((or (= char ?a) (= char ?A)) ; accept word without insert
  2164.             (ispell-send-string (concat "@" word "\n"))
  2165.             (if (null ispell-pdict-modified-p)
  2166.             (setq ispell-pdict-modified-p
  2167.                   (list ispell-pdict-modified-p)))
  2168.             (if (= char ?A) 0))    ; return 0 for ispell-add buffer-local
  2169.            ((or (= char ?r) (= char ?R)) ; type in replacement
  2170.             (and (eq 'block ispell-highlight-p) ; refresh tty's
  2171.              (ispell-highlight-spelling-error start end nil t))
  2172.             (let ((result
  2173.                (if (or (= char ?R) ispell-query-replace-choices)
  2174.                    (list (read-string
  2175.                       (format "Query-replacement for %s: "word)
  2176.                       word)
  2177.                      t)
  2178.                  (cons (read-string "Replacement for: " word)
  2179.                    nil))))
  2180.               (and (eq 'block ispell-highlight-p)
  2181.                (ispell-highlight-spelling-error start end nil
  2182.                                 'block))
  2183.               result))
  2184.            ((or (= char ??) (= char help-char) (= char ?\C-h))
  2185.             (and (eq 'block ispell-highlight-p)
  2186.              (ispell-highlight-spelling-error start end nil t))
  2187.             (ispell-help)
  2188.             (and (eq 'block ispell-highlight-p)
  2189.              (ispell-highlight-spelling-error start end nil
  2190.                               'block))
  2191.             t)
  2192.            ;; Quit and move point back.
  2193.            ((= char ?x)
  2194.             (ispell-pdict-save ispell-silently-savep)
  2195.             (message "Exited spell-checking")
  2196.             (setq ispell-quit t)
  2197.             nil)
  2198.            ;; Quit and preserve point.
  2199.            ((= char ?X)
  2200.             (ispell-pdict-save ispell-silently-savep)
  2201.             (message "%s"
  2202.              (substitute-command-keys
  2203.               (concat "Spell-checking suspended;"
  2204.                   " use C-u \\[ispell-word] to resume")))
  2205.             (setq ispell-quit start)
  2206.             nil)
  2207.            ((= char ?q)
  2208.             (if (y-or-n-p "Really kill Ispell process? ")
  2209.             (progn
  2210.               (ispell-kill-ispell t) ; terminate process.
  2211.               (setq ispell-quit (or (not ispell-checking-message)
  2212.                         (point))
  2213.                 ispell-pdict-modified-p nil))
  2214.               t))        ; continue if they don't quit.
  2215.            ((= char ?l)
  2216.             (and (eq 'block ispell-highlight-p) ; refresh tty displays
  2217.              (ispell-highlight-spelling-error start end nil t))
  2218.             (let ((new-word (read-string
  2219.                      "Lookup string (`*' is wildcard): "
  2220.                      word)))
  2221.               (if new-word
  2222.               (progn
  2223.                 (save-excursion
  2224.                   (set-buffer (get-buffer-create
  2225.                        ispell-choices-buffer))
  2226.                   (erase-buffer)
  2227.                   (setq count ?0
  2228.                     skipped 0
  2229.                     mode-line-format
  2230.                     (concat "--  %b  --  word: " new-word
  2231.                         "  --  dict: "
  2232.                         ispell-alternate-dictionary)
  2233.                     miss (lookup-words new-word)
  2234.                     choices miss
  2235.                     line ispell-choices-win-default-height)
  2236.                   (while (and choices ; adjust choices window.
  2237.                       (< (if (> (+ 7 (current-column)
  2238.                                (length (car choices))
  2239.                                (if (> count ?~) 3 0))
  2240.                             (window-width))
  2241.                          (progn
  2242.                            (insert "\n")
  2243.                            (setq line (1+ line)))
  2244.                            line)
  2245.                          max-lines))
  2246.                 (while (memq count command-characters)
  2247.                   (setq count (ispell-int-char (1+ count))
  2248.                     skipped (1+ skipped)))
  2249.                 (insert "(" count ") " (car choices) "  ")
  2250.                 (setq choices (cdr choices)
  2251.                       count (ispell-int-char (1+ count))))
  2252.                   (setq count (ispell-int-char
  2253.                        (- count ?0 skipped))))
  2254.                 (ispell-show-choices line end)
  2255.                 (select-window (next-window)))))
  2256.             (and (eq 'block ispell-highlight-p)
  2257.              (ispell-highlight-spelling-error start end nil
  2258.                               'block))
  2259.             t)            ; reselect from new choices
  2260.            ((= char ?u)        ; insert lowercase into dictionary
  2261.             (ispell-send-string (concat "*" (downcase word) "\n"))
  2262.             (setq ispell-pdict-modified-p '(t)) ; dictionary modified!
  2263.             nil)
  2264.            ((= char ?m)        ; type in what to insert
  2265.             (ispell-send-string
  2266.              (concat "*" (read-string "Insert: " word) "\n"))
  2267.             (setq ispell-pdict-modified-p '(t))
  2268.             (cons word nil))
  2269.            ((and (>= num 0) (< num count))
  2270.             (if ispell-query-replace-choices ; Query replace flag
  2271.             (list (nth num miss) 'query-replace)
  2272.               (nth num miss)))
  2273.            ((= char ?\C-l)
  2274.             (redraw-display) t)
  2275.            ((= char ?\C-r)
  2276.             ;; This may have alignment errors if current line is edited
  2277.             (if (marker-position ispell-recursive-edit-marker)
  2278.             (progn
  2279.               (message "Only one recursive edit session supported")
  2280.               (beep)
  2281.               (sit-for 2))
  2282.               (set-marker ispell-recursive-edit-marker start)
  2283.               ;;(set-marker ispell-region-end reg-end)
  2284.               (and ispell-highlight-p        ; unhighlight
  2285.                (ispell-highlight-spelling-error start end))
  2286.               (unwind-protect
  2287.               (progn
  2288.                 (message
  2289.                  "%s"
  2290.                  (substitute-command-keys
  2291.                   (concat "Exit recursive edit with"
  2292.                       " \\[exit-recursive-edit]")))
  2293.                 (save-window-excursion (save-excursion
  2294.                              (recursive-edit))))
  2295.             ;; protected
  2296.             (goto-char ispell-recursive-edit-marker)
  2297.             (if (not (equal (marker-buffer
  2298.                      ispell-recursive-edit-marker)
  2299.                     (current-buffer)))
  2300.                 (progn
  2301.                   (set-marker ispell-recursive-edit-marker nil)
  2302.                   (error
  2303.                    "Cannot continue ispell from this buffer.")))
  2304.             (set-marker ispell-recursive-edit-marker nil)))
  2305.             (list word nil))    ; recheck starting at this word.
  2306.            ((= char ?\C-z)
  2307.             (funcall (key-binding "\C-z"))
  2308.             t)
  2309.            (t (ding) t))))))
  2310.       result)
  2311.       ;; protected
  2312.       (and ispell-highlight-p        ; unhighlight
  2313.        (save-window-excursion
  2314.          (select-window textwin)
  2315.          (ispell-highlight-spelling-error start end)))
  2316.       (if dedicated
  2317.       (set-window-dedicated-p dedicated-win t)))))
  2318.  
  2319.  
  2320.  
  2321. (defun ispell-show-choices (line end)
  2322.   "Show the choices in another buffer or frame."
  2323.   (if (and ispell-use-framepop-p (fboundp 'framepop-display-buffer))
  2324.       (progn
  2325.     (framepop-display-buffer (get-buffer ispell-choices-buffer))
  2326. ;;;    (get-buffer-window ispell-choices-buffer t)
  2327.     (select-window (previous-window))) ; *Choices* window
  2328.     ;; standard selection by splitting a small buffer out of this window.
  2329.     (let ((choices-window (get-buffer-window ispell-choices-buffer)))
  2330.       (if choices-window
  2331.       (if (= line (ispell-adjusted-window-height choices-window))
  2332.           (select-window choices-window)
  2333.         ;; *Choices* window changed size.  Adjust the choices window
  2334.         ;; without scrolling the spelled window when possible
  2335.         (let ((window-line
  2336.            (- line (ispell-adjusted-window-height choices-window)))
  2337.           (visible (progn (vertical-motion -1) (point))))
  2338.           (if (< line ispell-choices-win-default-height)
  2339.           (setq window-line (+ window-line
  2340.                        (- ispell-choices-win-default-height
  2341.                       line))))
  2342.           (move-to-window-line 0)
  2343.           (vertical-motion window-line)
  2344.           (set-window-start (selected-window)
  2345.                 (if (> (point) visible) visible (point)))
  2346.           (goto-char end)
  2347.           (select-window choices-window)
  2348.           (enlarge-window window-line)))
  2349.     ;; Overlay *Choices* window when it isn't showing
  2350.     (ispell-overlay-window (max line ispell-choices-win-default-height)))
  2351.       (switch-to-buffer ispell-choices-buffer)
  2352.       (goto-char (point-min)))))
  2353.  
  2354.  
  2355. ;;;###autoload
  2356. (defun ispell-help ()
  2357.   "Display a list of the options available when a misspelling is encountered.
  2358.  
  2359. Selections are:
  2360.  
  2361. DIGIT: Replace the word with a digit offered in the *Choices* buffer.
  2362. SPC:   Accept word this time.
  2363. `i':   Accept word and insert into private dictionary.
  2364. `a':   Accept word for this session.
  2365. `A':   Accept word and place in `buffer-local dictionary'.
  2366. `r':   Replace word with typed-in value.  Rechecked.
  2367. `R':   Replace word with typed-in value. Query-replaced in buffer. Rechecked.
  2368. `?':   Show these commands.
  2369. `x':   Exit spelling buffer.  Move cursor to original point.
  2370. `X':   Exit spelling buffer.  Leaves cursor at the current point, and permits
  2371.         the aborted check to be completed later.
  2372. `q':   Quit spelling session (Kills ispell process).
  2373. `l':   Look up typed-in replacement in alternate dictionary.  Wildcards okay.
  2374. `u':   Like `i', but the word is lower-cased first.
  2375. `m':   Place typed-in value in personal dictionary, then recheck current word.
  2376. `C-l':  Redraw screen.
  2377. `C-r':  Recursive edit.
  2378. `C-z':  Suspend Emacs or iconify frame."
  2379.  
  2380.   (if (equal ispell-help-in-bufferp 'electric)
  2381.       (progn
  2382.     (require 'ehelp)
  2383.     (with-electric-help
  2384.      (function (lambda ()
  2385.              ;;This shouldn't be necessary: with-electric-help needs
  2386.              ;; an optional argument telling it about the smallest
  2387.              ;; acceptable window-height of the help buffer.
  2388.              ;;(if (< (window-height) 15)
  2389.              ;;     (enlarge-window
  2390.              ;;      (- 15 (ispell-adjusted-window-height))))
  2391.              (princ "Selections are:
  2392.  
  2393. DIGIT: Replace the word with a digit offered in the *Choices* buffer.
  2394. SPC:   Accept word this time.
  2395. `i':   Accept word and insert into private dictionary.
  2396. `a':   Accept word for this session.
  2397. `A':   Accept word and place in `buffer-local dictionary'.
  2398. `r':   Replace word with typed-in value.  Rechecked.
  2399. `R':   Replace word with typed-in value. Query-replaced in buffer. Rechecked.
  2400. `?':   Show these commands.
  2401. `x':   Exit spelling buffer.  Move cursor to original point.
  2402. `X':   Exit spelling buffer.  Leaves cursor at the current point, and permits
  2403.         the aborted check to be completed later.
  2404. `q':   Quit spelling session (Kills ispell process).
  2405. `l':   Look up typed-in replacement in alternate dictionary.  Wildcards okay.
  2406. `u':   Like `i', but the word is lower-cased first.
  2407. `m':   Place typed-in value in personal dictionary, then recheck current word.
  2408. `C-l':  Redraw screen.
  2409. `C-r':  Recursive edit.
  2410. `C-z':  Suspend Emacs or iconify frame.")
  2411.              nil))))
  2412.  
  2413.  
  2414.     (let ((help-1 (concat "[r/R]eplace word; [a/A]ccept for this session; "
  2415.               "[i]nsert into private dictionary"))
  2416.       (help-2 (concat "[l]ook a word up in alternate dictionary;  "
  2417.               "e[x/X]it;  [q]uit session"))
  2418.       (help-3 (concat "[u]ncapitalized insert into dict.  "
  2419.               "Type 'x C-h f ispell-help' for more help")))
  2420.       (save-window-excursion
  2421.     (if ispell-help-in-bufferp
  2422.         (progn
  2423.           (ispell-overlay-window 4)
  2424.           (switch-to-buffer (get-buffer-create "*Ispell Help*"))
  2425.           (insert (concat help-1 "\n" help-2 "\n" help-3))
  2426.           (sit-for 5)
  2427.           (kill-buffer "*Ispell Help*"))
  2428.       (unwind-protect
  2429.           (let ((resize-mini-windows 'grow-only))
  2430.         (select-window (minibuffer-window))
  2431.         (erase-buffer)
  2432.         (message nil)
  2433.         ;;(set-minibuffer-window (selected-window))
  2434.         (enlarge-window 2)
  2435.         (insert (concat help-1 "\n" help-2 "\n" help-3))
  2436.         (sit-for 5))
  2437.         (erase-buffer)))))))
  2438.  
  2439.  
  2440. (defun lookup-words (word &optional lookup-dict)
  2441.   "Look up WORD in optional word-list dictionary LOOKUP-DICT.
  2442. A `*' serves as a wild card.  If no wild cards, `look' is used if it exists.
  2443. Otherwise the variable `ispell-grep-command' contains the command used to
  2444. search for the words (usually egrep).
  2445.  
  2446. Optional second argument contains the dictionary to use; the default is
  2447. `ispell-alternate-dictionary'."
  2448.   ;; We don't use the filter for this function, rather the result is written
  2449.   ;; into a buffer.  Hence there is no need to save the filter values.
  2450.   (if (null lookup-dict)
  2451.       (setq lookup-dict ispell-alternate-dictionary))
  2452.  
  2453.   (let* ((process-connection-type ispell-use-ptys-p)
  2454.      (wild-p (string-match "\\*" word))
  2455.      (look-p (and ispell-look-p    ; Only use look for an exact match.
  2456.               (or ispell-have-new-look (not wild-p))))
  2457.      (ispell-grep-buffer (get-buffer-create "*Ispell-Temp*")) ; result buf
  2458.      (prog (if look-p ispell-look-command ispell-grep-command))
  2459.      (args (if look-p ispell-look-options ispell-grep-options))
  2460.      status results loc)
  2461.     (unwind-protect
  2462.     (save-window-excursion
  2463.       (message "Starting \"%s\" process..." (file-name-nondirectory prog))
  2464.       (set-buffer ispell-grep-buffer)
  2465.       (if look-p
  2466.           nil
  2467.         ;; convert * to .*
  2468.         (insert "^" word "$")
  2469.         (while (search-backward "*" nil t) (insert "."))
  2470.         (setq word (buffer-string))
  2471.         (erase-buffer))
  2472.       (setq status (apply 'ispell-call-process prog nil t nil
  2473.                   (nconc (if (and args (> (length args) 0))
  2474.                      (list args)
  2475.                        (if look-p nil
  2476.                      (list "-e")))
  2477.                      (list word)
  2478.                      (if lookup-dict (list lookup-dict)))))
  2479.       ;; grep returns status 1 and no output when word not found, which
  2480.       ;; is a perfectly normal thing.
  2481.       (if (stringp status)
  2482.           (setq results (cons (format "error: %s exited with signal %s"
  2483.                       (file-name-nondirectory prog) status)
  2484.                   results))
  2485.         ;; else collect words into `results' in FIFO order
  2486.         (goto-char (point-max))
  2487.         ;; assure we've ended with \n
  2488.         (or (bobp) (= (preceding-char) ?\n) (insert ?\n))
  2489.         (while (not (bobp))
  2490.           (setq loc (point))
  2491.           (forward-line -1)
  2492.           (setq results (cons (buffer-substring-no-properties (point)
  2493.                                   (1- loc))
  2494.                   results)))))
  2495.       ;; protected
  2496.       (kill-buffer ispell-grep-buffer)
  2497.       (if (and results (string-match ".+: " (car results)))
  2498.       (error "%s error: %s" ispell-grep-command (car results))))
  2499.     results))
  2500.  
  2501.  
  2502. ;;; "ispell-filter" is a list of output lines from the generating function.
  2503. ;;;   Each full line (ending with \n) is a separate item on the list.
  2504. ;;; "output" can contain multiple lines, part of a line, or both.
  2505. ;;; "start" and "end" are used to keep bounds on lines when "output" contains
  2506. ;;;   multiple lines.
  2507. ;;; "ispell-filter-continue" is true when we have received only part of a
  2508. ;;;   line as output from a generating function ("output" did not end with \n)
  2509. ;;; THIS FUNCTION WILL FAIL IF THE PROCESS OUTPUT DOESN'T END WITH \n!
  2510. ;;;   This is the case when a process dies or fails. The default behavior
  2511. ;;;   in this case treats the next input received as fresh input.
  2512.  
  2513. (defun ispell-filter (process output)
  2514.   "Output filter function for ispell, grep, and look."
  2515.   (let ((start 0)
  2516.     (continue t)
  2517.     end)
  2518.     (while continue
  2519.       (setq end (string-match "\n" output start)) ; get text up to the newline.
  2520.       ;; If we get out of sync and ispell-filter-continue is asserted when we
  2521.       ;; are not continuing, treat the next item as a separate list.  When
  2522.       ;; ispell-filter-continue is asserted, ispell-filter *should* always be a
  2523.       ;; list!
  2524.  
  2525.       ;; Continue with same line (item)?
  2526.       (if (and ispell-filter-continue ispell-filter (listp ispell-filter))
  2527.       ;; Yes.  Add it to the prev item
  2528.       (setcar ispell-filter
  2529.           (concat (car ispell-filter) (substring output start end)))
  2530.     ;; No. This is a new line and item.
  2531.     (setq ispell-filter
  2532.           (cons (substring output start end) ispell-filter)))
  2533.       (if (null end)
  2534.       ;; We've completed reading the output, but didn't finish the line.
  2535.       (setq ispell-filter-continue t continue nil)
  2536.     ;; skip over newline, this line complete.
  2537.     (setq ispell-filter-continue nil end (1+ end))
  2538.     (if (= end (length output))    ; No more lines in output
  2539.         (setq continue nil)        ;  so we can exit the filter.
  2540.       (setq start end))))))        ; else move start to next line of input
  2541.  
  2542.  
  2543. ;;; This function destroys the mark location if it is in the word being
  2544. ;;; highlighted.
  2545. (defun ispell-highlight-spelling-error-generic (start end &optional highlight
  2546.                               refresh)
  2547.   "Highlight the word from START to END with a kludge using `inverse-video'.
  2548. When the optional third arg HIGHLIGHT is set, the word is highlighted;
  2549. otherwise it is displayed normally.
  2550. Uses block cursor to highlight one character.
  2551. Optional REFRESH will unhighlighted then highlight, using block cursor
  2552.  highlighting when REFRESH is equal to `block'."
  2553.   (and (eq 'block ispell-highlight-p)
  2554.        (or (eq 'block refresh)
  2555.        (setq start (1+ start))))    ; On block non-refresh, inc start.
  2556.   (let ((modified (buffer-modified-p))    ; don't allow this fn to modify buffer
  2557.     (buffer-read-only nil)        ; Allow highlighting read-only buffers.
  2558.     (text (buffer-substring-no-properties start end)) ; Save hilight region
  2559.     (inhibit-quit t)        ; inhibit interrupt processing here.
  2560.     (buffer-undo-list t))        ; don't clutter the undo list.
  2561.     (goto-char end)
  2562.     (delete-region start end)
  2563.     (insert-char ?  (- end start))    ; minimize amount of redisplay
  2564.     (sit-for 0)                ; update display
  2565.     (if highlight (setq inverse-video (not inverse-video))) ; toggle video
  2566.     (delete-region start end)        ; delete whitespace
  2567.     (insert text)            ; insert text in inverse video.
  2568.     (sit-for 0)                ; update display showing inverse video.
  2569.     (if (not highlight)
  2570.     (goto-char end)
  2571.       (setq inverse-video (not inverse-video)) ; toggle video
  2572.       (and (eq 'block ispell-highlight-p)
  2573.        (goto-char (1- start))))    ; use block cursor to "highlight" char
  2574.     (set-buffer-modified-p modified)    ; don't modify if flag not set.
  2575.     (and refresh            ; re-highlight
  2576.      (ispell-highlight-spelling-error-generic
  2577.       (if (eq 'block refresh) start (- start 2)) end t))))
  2578.  
  2579.  
  2580. (defun ispell-highlight-spelling-error-xemacs (start end &optional highlight)
  2581.   "Highlight the word from START to END using `isearch-highlight'.
  2582. When the optional third arg HIGHLIGHT is set, the word is highlighted,
  2583. otherwise it is displayed normally."
  2584.   (if highlight
  2585.       (isearch-highlight start end)
  2586.     (isearch-dehighlight))
  2587.   ;;(sit-for 0)
  2588.   )
  2589.  
  2590.  
  2591. (defun ispell-highlight-spelling-error-overlay (start end &optional highlight)
  2592.   "Highlight the word from START to END using overlays.
  2593. When the optional third arg HIGHLIGHT is set, the word is highlighted
  2594. otherwise it is displayed normally.
  2595.  
  2596. The variable `ispell-highlight-face' selects the face to use for highlighting."
  2597.   (if highlight
  2598.       (if ispell-overlay
  2599.       (move-overlay ispell-overlay start end (current-buffer))
  2600.     (setq ispell-overlay (make-overlay start end))
  2601.     (overlay-put ispell-overlay 'priority 1001) ;higher than lazy overlays
  2602.     (overlay-put ispell-overlay 'face ispell-highlight-face))
  2603.     (if ispell-overlay
  2604.     (delete-overlay ispell-overlay)))
  2605.   (if (and ispell-lazy-highlight (boundp 'lazy-highlight-cleanup))
  2606.       (if highlight
  2607.       (let ((isearch-string
  2608.          (concat
  2609.           "\\b"
  2610.           (regexp-quote (buffer-substring-no-properties start end))
  2611.           "\\b"))
  2612.         (isearch-regexp t)
  2613.         (isearch-case-fold-search nil))
  2614.         (isearch-lazy-highlight-new-loop
  2615.          (if (boundp 'reg-start) reg-start)
  2616.          (if (boundp 'reg-end)   reg-end)))
  2617.     (lazy-highlight-cleanup lazy-highlight-cleanup)
  2618.     (setq isearch-lazy-highlight-last-string nil))))
  2619.  
  2620.  
  2621. (defun ispell-highlight-spelling-error (start end &optional highlight refresh)
  2622.   (cond
  2623.    ((featurep 'xemacs)
  2624.     (ispell-highlight-spelling-error-xemacs start end highlight))
  2625.    ((and (featurep 'faces)
  2626.      (or (and (fboundp 'display-color-p) (display-color-p))
  2627.          window-system))
  2628.     (ispell-highlight-spelling-error-overlay start end highlight))
  2629.    (t (ispell-highlight-spelling-error-generic start end highlight refresh))))
  2630.  
  2631. (defun ispell-adjusted-window-height (&optional window)
  2632.   "Like `window-height', adjusted to correct for the effect of tall mode-lines.
  2633. The value returned is actually the nominal number of text-lines in the
  2634. window plus 1.  On a terminal, this is the same value returned by
  2635. `window-height', but if the window has a mode-line is taller than a normal
  2636. text line, the returned value may be smaller than that from
  2637. `window-height'."
  2638.   (cond ((fboundp 'window-text-height)
  2639.      (1+ (window-text-height window)))
  2640.     ((or (and (fboundp 'display-graphic-p) (display-graphic-p))
  2641.          (and (featurep 'xemacs) window-system))
  2642.      (1- (window-height window)))
  2643.     (t
  2644.      (window-height window))))
  2645.  
  2646. (defun ispell-overlay-window (height)
  2647.   "Create a window covering the top HEIGHT lines of the current window.
  2648. Ensure that the line above point is still visible but otherwise avoid
  2649. scrolling the current window.  Leave the new window selected."
  2650.   (save-excursion
  2651.     (let ((oldot (save-excursion (vertical-motion -1) (point)))
  2652.       (top (save-excursion (move-to-window-line height) (point))))
  2653.       ;; If line above old point (line starting at oldot) would be
  2654.       ;; hidden by new window, scroll it to just below new win
  2655.       ;; otherwise set top line of other win so it doesn't scroll.
  2656.       (if (< oldot top) (setq top oldot))
  2657.       ;; if frame is unsplitable, temporarily disable that...
  2658.       (if (cdr (assq 'unsplittable (frame-parameters (selected-frame))))
  2659.       (let ((frame (selected-frame)))
  2660.         (modify-frame-parameters frame '((unsplittable . nil)))
  2661.         (split-window nil height)
  2662.         (modify-frame-parameters frame '((unsplittable . t))))
  2663.     (split-window nil height))
  2664.       (let ((deficit (- height (ispell-adjusted-window-height))))
  2665.     (when (> deficit 0)
  2666.       ;; Number of lines the window is still too short.  We ensure that
  2667.       ;; there are at least (1- HEIGHT) lines visible in the window.
  2668.       (enlarge-window deficit)
  2669.       (goto-char top)
  2670.       (vertical-motion deficit)
  2671.       (setq top (min (point) oldot))))
  2672.       (set-window-start (next-window) top))))
  2673.  
  2674.  
  2675. ;;; Should we add a compound word match return value?
  2676. (defun ispell-parse-output (output &optional accept-list shift)
  2677.   "Parse the OUTPUT string from Ispell process and return:
  2678. 1: t for an exact match.
  2679. 2: A string containing the root word matched via suffix removal.
  2680. 3: A list of possible correct spellings of the format:
  2681.    (\"ORIGINAL-WORD\" OFFSET MISS-LIST GUESS-LIST)
  2682.    ORIGINAL-WORD is a string of the possibly misspelled word.
  2683.    OFFSET is an integer giving the line offset of the word.
  2684.    MISS-LIST and GUESS-LIST are possibly null lists of guesses and misses.
  2685. 4: nil when an error has occurred.
  2686.  
  2687. Optional second arg ACCEPT-LIST is list of words already accepted.
  2688. Optional third arg SHIFT is an offset to apply based on previous corrections."
  2689.   (cond
  2690.    ((string= output "") t)        ; for startup with pipes...
  2691.    ((string= output "*") t)        ; exact match
  2692.    ((string= output "-") t)        ; compound word match
  2693.    ((eq (aref output 0) ?+)        ; found because of root word
  2694.     (substring output 2))        ; return root word
  2695.    ((equal 0 (string-match "[\ra-zA-Z]" output))
  2696.     (ding)                ; error message from ispell!
  2697.     (message "Ispell error: %s" output)
  2698.     (sit-for 5)
  2699.     nil)
  2700.    (t                    ; need to process &, ?, and #'s
  2701.     (let ((type (aref output 0))    ; &, ?, or #
  2702.       (original-word (substring output 2 (string-match " " output 2)))
  2703.       (cur-count 0)            ; contains number of misses + guesses
  2704.       count miss-list guess-list offset)
  2705.       (setq output (substring output (match-end 0))) ; skip over misspelling
  2706.       (if (eq type ?#)
  2707.       (setq count 0)        ; no misses for type #
  2708.     (setq count (string-to-number output) ; get number of misses.
  2709.           output (substring output (1+ (string-match " " output 1)))))
  2710.       (setq offset (string-to-number output))
  2711.       (if (eq type ?#)            ; No miss or guess list.
  2712.       (setq output nil)
  2713.     (setq output (substring output (1+ (string-match " " output 1)))))
  2714.       (while output
  2715.     (let ((end (string-match ", \\|\\($\\)" output))) ; end of miss/guess.
  2716.       (setq cur-count (1+ cur-count))
  2717.       (if (> cur-count count)
  2718.           (setq guess-list (cons (substring output 0 end) guess-list))
  2719.         (setq miss-list (cons (substring output 0 end) miss-list)))
  2720.       (if (match-end 1)        ; True only when at end of line.
  2721.           (setq output nil)        ; no more misses or guesses
  2722.         (setq output (substring output (+ end 2))))))
  2723.       ;; return results.  Accept word if it was already accepted.
  2724.       ;; adjust offset.
  2725.       (if (member original-word accept-list)
  2726.       t
  2727.     (list original-word
  2728.           (if (numberp shift) (+ shift offset) offset)
  2729.           (nreverse miss-list) (nreverse guess-list)))))))
  2730.  
  2731.  
  2732. (defun ispell-process-status ()
  2733.   "Return the status of the Ispell process.
  2734. When asynchronous processes are not supported, `run' is always returned."
  2735.   (if ispell-async-processp
  2736.       (process-status ispell-process)
  2737.     (and ispell-process 'run)))
  2738.  
  2739.  
  2740. (defun ispell-start-process ()
  2741.   "Start the ispell process, with support for no asynchronous processes.
  2742. Keeps argument list for future ispell invocations for no async support."
  2743.   (let ((default-directory default-directory)
  2744.     args)
  2745.     (unless (and (file-directory-p default-directory)
  2746.          (file-readable-p default-directory))
  2747.       ;; Defend against bad `default-directory'.
  2748.       (setq default-directory (expand-file-name "~/")))
  2749.     ;; Local dictionary becomes the global dictionary in use.
  2750.     (setq ispell-current-dictionary
  2751.       (or ispell-local-dictionary ispell-dictionary))
  2752.     (setq ispell-current-personal-dictionary
  2753.       (or ispell-local-pdict ispell-personal-dictionary))
  2754.     (setq args (ispell-get-ispell-args))
  2755.     (if (and ispell-current-dictionary    ; use specified dictionary
  2756.          (not (member "-d" args)))    ; only define if not overridden
  2757.     (setq args
  2758.           (append (list "-d" ispell-current-dictionary) args)))
  2759.     (if ispell-current-personal-dictionary    ; use specified pers dict
  2760.     (setq args
  2761.           (append args
  2762.               (list "-p"
  2763.                 (expand-file-name ispell-current-personal-dictionary)))))
  2764.  
  2765.     ;; If we are using recent aspell or hunspell, make sure we use the right encoding
  2766.     ;; for communication. ispell or older aspell/hunspell does not support this
  2767.     (if ispell-encoding8-command
  2768.     (setq args
  2769.           (append args
  2770.               (list
  2771.                (concat ispell-encoding8-command
  2772.                    (symbol-name (ispell-get-coding-system)))))))
  2773.     (setq args (append args ispell-extra-args))
  2774.  
  2775.     ;; Initially we don't know any buffer's local words.
  2776.     (setq ispell-buffer-local-name nil)
  2777.  
  2778.     (if ispell-async-processp
  2779.     (let ((process-connection-type ispell-use-ptys-p))
  2780.       (apply 'start-process
  2781.          "ispell" nil ispell-program-name
  2782.          "-a"                         ; accept single input lines
  2783.          (if ispell-really-hunspell "" "-m") ; make root/affix combos not in dict
  2784.          args))                              ; hunspell -m option means different
  2785.       (setq ispell-cmd-args args
  2786.         ispell-output-buffer (generate-new-buffer " *ispell-output*")
  2787.         ispell-session-buffer (generate-new-buffer " *ispell-session*"))
  2788.       (ispell-send-string "\032\n")    ; so Ispell prints version and exits
  2789.       t)))
  2790.  
  2791.  
  2792.  
  2793. (defun ispell-init-process ()
  2794.   "Check status of Ispell process and start if necessary."
  2795.   (if (and ispell-process
  2796.        (eq (ispell-process-status) 'run)
  2797.        ;; If we're using a personal dictionary, ensure
  2798.        ;; we're in the same default directory!
  2799.        (or (not ispell-personal-dictionary)
  2800.            (equal ispell-process-directory default-directory)))
  2801.       (setq ispell-filter nil ispell-filter-continue nil)
  2802.     ;; may need to restart to select new personal dictionary.
  2803.     (ispell-kill-ispell t)
  2804.     (message "Starting new Ispell process [%s] ..."
  2805.          (or ispell-local-dictionary ispell-dictionary "default"))
  2806.     (sit-for 0)
  2807.     (setq ispell-library-directory (ispell-check-version)
  2808.       ispell-process-directory default-directory
  2809.       ispell-process (ispell-start-process)
  2810.       ispell-filter nil
  2811.       ispell-filter-continue nil)
  2812.     (if ispell-async-processp
  2813.     (set-process-filter ispell-process 'ispell-filter))
  2814.     ;; protect against bogus binding of `enable-multibyte-characters' in XEmacs
  2815.     (if (and (or (featurep 'xemacs)
  2816.          (and (boundp 'enable-multibyte-characters)
  2817.               enable-multibyte-characters))
  2818.          (fboundp 'set-process-coding-system))
  2819.     (set-process-coding-system ispell-process (ispell-get-coding-system)
  2820.                    (ispell-get-coding-system)))
  2821.     ;; Get version ID line
  2822.     (ispell-accept-output 3)
  2823.     ;; get more output if filter empty?
  2824.     (if (null ispell-filter) (ispell-accept-output 3))
  2825.     (cond ((null ispell-filter)
  2826.        (error "%s did not output version line" ispell-program-name))
  2827.       ((and
  2828.         (stringp (car ispell-filter))
  2829.         (if (string-match "warning: " (car ispell-filter))
  2830.         (progn
  2831.           (ispell-accept-output 3) ; was warn msg.
  2832.           (stringp (car ispell-filter)))
  2833.           (null (cdr ispell-filter)))
  2834.         (string-match "^@(#) " (car ispell-filter)))
  2835.        ;; got the version line as expected (we already know it's the right
  2836.        ;; version, so don't bother checking again.)
  2837.        nil)
  2838.       (t
  2839.        ;; Otherwise, it must be an error message.  Show the user.
  2840.        ;; But first wait to see if some more output is going to arrive.
  2841.        ;; Otherwise we get cool errors like "Can't open ".
  2842.        (sleep-for 1)
  2843.        (ispell-accept-output 3)
  2844.        (error "%s" (mapconcat 'identity ispell-filter "\n"))))
  2845.     (setq ispell-filter nil)        ; Discard version ID line
  2846.     (let ((extended-char-mode (ispell-get-extended-character-mode)))
  2847.       (if extended-char-mode        ; ~ extended character mode
  2848.       (ispell-send-string (concat extended-char-mode "\n"))))
  2849.     (if ispell-async-processp
  2850.     (if (fboundp 'set-process-query-on-exit-flag)
  2851.         (set-process-query-on-exit-flag ispell-process nil)
  2852.       (if (fboundp 'process-kill-without-query)
  2853.           (process-kill-without-query ispell-process))))))
  2854.  
  2855. ;;;###autoload
  2856. (defun ispell-kill-ispell (&optional no-error)
  2857.   "Kill current Ispell process (so that you may start a fresh one).
  2858. With NO-ERROR, just return non-nil if there was no Ispell running."
  2859.   (interactive)
  2860.   ;; This hook is typically used by flyspell to flush some variables used
  2861.   ;; to optimize the common cases.
  2862.   (run-hooks 'ispell-kill-ispell-hook)
  2863.   (if (not (and ispell-process
  2864.         (eq (ispell-process-status) 'run)))
  2865.       (or no-error
  2866.       (error "There is no ispell process running!"))
  2867.     (if ispell-async-processp
  2868.     ;; --- Debian changes: Work around emacs21 race condition
  2869.     ;; (delete-process ispell-process)
  2870.     (ispell-delete-ispell-process)
  2871.       ;; --- End of Debian changes
  2872.       ;; synchronous processes
  2873.       (ispell-send-string "\n")        ; make sure side effects occurred.
  2874.       (kill-buffer ispell-output-buffer)
  2875.       (kill-buffer ispell-session-buffer)
  2876.       (setq ispell-output-buffer nil
  2877.         ispell-session-buffer nil))
  2878.     (setq ispell-process nil)
  2879.     (message "Ispell process killed")
  2880.     nil))
  2881.  
  2882.  
  2883. ;;; ispell-change-dictionary is set in some people's hooks.  Maybe this should
  2884. ;;;  call ispell-init-process rather than wait for a spell checking command?
  2885.  
  2886. ;;;###autoload
  2887. (defun ispell-change-dictionary (dict &optional arg)
  2888.   "Change to dictionary DICT for Ispell.
  2889. With a prefix arg, set it \"globally\", for all buffers.
  2890. Without a prefix arg, set it \"locally\", just for this buffer.
  2891.  
  2892. By just answering RET you can find out what the current dictionary is."
  2893.   (interactive
  2894.    (list (completing-read
  2895.       "Use new dictionary (RET for current, SPC to complete): "
  2896.       (and (fboundp 'ispell-valid-dictionary-list)
  2897.            (mapcar 'list (ispell-valid-dictionary-list)))
  2898.       nil t)
  2899.      current-prefix-arg))
  2900.   (ispell-set-spellchecker-params) ; Initilize variables and dicts alists
  2901.   (unless arg (ispell-buffer-local-dict 'no-reload))
  2902.   (if (equal dict "default") (setq dict nil))
  2903.   ;; This relies on completing-read's bug of returning "" for no match
  2904.   (cond ((equal dict "")
  2905.      (ispell-internal-change-dictionary)
  2906.      (message "Using %s dictionary"
  2907.           (or (and (not arg) ispell-local-dictionary)
  2908.               ispell-dictionary "default")))
  2909.     ((equal dict (or (and (not arg) ispell-local-dictionary)
  2910.              ispell-dictionary "default"))
  2911.      ;; Specified dictionary is the default already. Could reload
  2912.      ;; the dictionaries if needed.
  2913.      (ispell-internal-change-dictionary)
  2914.      (and (interactive-p)
  2915.           (message "No change, using %s dictionary" dict)))
  2916.     (t                ; reset dictionary!
  2917.      (if (or (assoc dict ispell-local-dictionary-alist)
  2918.          (assoc dict ispell-dictionary-alist))
  2919.          (if arg
  2920.          ;; set default dictionary
  2921.          (setq ispell-dictionary dict)
  2922.            ;; set local dictionary
  2923.            (setq ispell-local-dictionary dict)
  2924.            (setq ispell-local-dictionary-overridden t))
  2925.        (error "Undefined dictionary: %s" dict))
  2926.      (ispell-internal-change-dictionary)
  2927.      (message "%s Ispell dictionary set to %s"
  2928.           (if arg "Global" "Local")
  2929.           dict))))
  2930.  
  2931. (defun ispell-internal-change-dictionary ()
  2932.   "Update the dictionary and the personal dictionary used by Ispell.
  2933. This may kill the Ispell process; if so,
  2934. a new one will be started when needed."
  2935.   (let ((dict (or ispell-local-dictionary ispell-dictionary))
  2936.     (pdict (or ispell-local-pdict ispell-personal-dictionary)))
  2937.     (unless (and (equal ispell-current-dictionary dict)
  2938.          (equal ispell-current-personal-dictionary pdict))
  2939.       (ispell-kill-ispell t)
  2940.       (setq ispell-current-dictionary dict
  2941.         ispell-current-personal-dictionary pdict))))
  2942.  
  2943. ;;; Spelling of comments are checked when ispell-check-comments is non-nil.
  2944.  
  2945. ;;;###autoload
  2946. (defun ispell-region (reg-start reg-end &optional recheckp shift)
  2947.   "Interactively check a region for spelling errors.
  2948. Return nil if spell session is quit,
  2949.  otherwise returns shift offset amount for last line processed."
  2950.   (interactive "r")            ; Don't flag errors on read-only bufs.
  2951.   (ispell-set-spellchecker-params)      ; Initialize variables and dicts alists
  2952.   (if (not recheckp)
  2953.       (ispell-accept-buffer-local-defs)) ; set up dictionary, local words, etc.
  2954.   (let ((skip-region-start (make-marker))
  2955.     (rstart (make-marker)))
  2956.   (unwind-protect
  2957.       (save-excursion
  2958.     (message "Spell-checking %s using %s with %s dictionary..."
  2959.          (if (and (= reg-start (point-min)) (= reg-end (point-max)))
  2960.              (buffer-name) "region")
  2961.          (file-name-nondirectory ispell-program-name)
  2962.          (or ispell-current-dictionary "default"))
  2963.     ;; Returns cursor to original location.
  2964.     (save-window-excursion
  2965.       (goto-char reg-start)
  2966.       (let ((transient-mark-mode)
  2967.         (case-fold-search case-fold-search)
  2968.         (query-fcc t)
  2969.         in-comment key)
  2970.         (let (message-log-max)
  2971.           (message "searching for regions to skip"))
  2972.         (if (re-search-forward (ispell-begin-skip-region-regexp) reg-end t)
  2973.         (progn
  2974.           (setq key (buffer-substring-no-properties
  2975.                  (match-beginning 0) (match-end 0)))
  2976.           (set-marker skip-region-start (- (point) (length key)))
  2977.           (goto-char reg-start)))
  2978.         (let (message-log-max)
  2979.           (message "Continuing spelling check using %s with %s dictionary..."
  2980.                (file-name-nondirectory ispell-program-name)
  2981.                (or ispell-current-dictionary "default")))
  2982.         (set-marker rstart reg-start)
  2983.         (set-marker ispell-region-end reg-end)
  2984.         (while (and (not ispell-quit)
  2985.             (< (point) ispell-region-end))
  2986.           ;; spell-check region with skipping
  2987.           (if (and (marker-position skip-region-start)
  2988.                (<= skip-region-start (point)))
  2989.           (progn
  2990.             ;; If region inside line comment, must keep comment start.
  2991.             (setq in-comment (point)
  2992.               in-comment
  2993.               (and comment-start
  2994.                    (or (null comment-end) (string= "" comment-end))
  2995.                    (save-excursion
  2996.                  (beginning-of-line)
  2997.                  (re-search-forward comment-start in-comment t))
  2998.                    comment-start))
  2999.             ;; Can change skip-regexps (in ispell-message)
  3000.             (ispell-skip-region key) ; moves pt past region.
  3001.             (set-marker rstart (point))
  3002.             ;; check for saving large attachments...
  3003.             (setq query-fcc (and query-fcc
  3004.                      (ispell-ignore-fcc skip-region-start
  3005.                                 rstart)))
  3006.             (if (and (< rstart ispell-region-end)
  3007.                  (re-search-forward
  3008.                   (ispell-begin-skip-region-regexp)
  3009.                   ispell-region-end t))
  3010.             (progn
  3011.               (setq key (match-string-no-properties 0))
  3012.               (set-marker skip-region-start
  3013.                       (- (point) (length key)))
  3014.               (goto-char rstart))
  3015.               (set-marker skip-region-start nil))))
  3016.           (setq reg-end (max (point)
  3017.                  (if (marker-position skip-region-start)
  3018.                      (min skip-region-start ispell-region-end)
  3019.                    (marker-position ispell-region-end))))
  3020.           (let* ((start (point))
  3021.              (end (save-excursion (end-of-line) (min (point) reg-end)))
  3022.              (string (ispell-get-line start end in-comment)))
  3023.         (if in-comment        ; account for comment chars added
  3024.             (setq start (- start (length in-comment))
  3025.               in-comment nil))
  3026.         (setq end (point))    ; "end" tracks region retrieved.
  3027.         (if string        ; there is something to spell check!
  3028.             ;; (special start end)
  3029.             (setq shift (ispell-process-line string
  3030.                              (and recheckp shift))))
  3031.         (goto-char end)))))
  3032.     (if ispell-quit
  3033.         nil
  3034.       (or shift 0)))
  3035.     ;; protected
  3036.     (if (and (not (and recheckp ispell-keep-choices-win))
  3037.          (get-buffer ispell-choices-buffer))
  3038.     (kill-buffer ispell-choices-buffer))
  3039.     (set-marker skip-region-start nil)
  3040.     (set-marker rstart nil)
  3041.     (if ispell-quit
  3042.     (progn
  3043.       ;; preserve or clear the region for ispell-continue.
  3044.       (if (not (numberp ispell-quit))
  3045.           (set-marker ispell-region-end nil)
  3046.         ;; Ispell-continue enabled - ispell-region-end is set.
  3047.         (goto-char ispell-quit))
  3048.       ;; Check for aborting
  3049.       (if (and ispell-checking-message (numberp ispell-quit))
  3050.           (progn
  3051.         (setq ispell-quit nil)
  3052.         (error "Message send aborted")))
  3053.       (if (not recheckp) (setq ispell-quit nil)))
  3054.       (if (not recheckp) (set-marker ispell-region-end nil))
  3055.       ;; Only save if successful exit.
  3056.       (ispell-pdict-save ispell-silently-savep)
  3057.       (message "Spell-checking %s using %s with %s dictionary done"
  3058.            (if (and (= reg-start (point-min)) (= reg-end (point-max)))
  3059.            (buffer-name) "region")
  3060.            (file-name-nondirectory ispell-program-name)
  3061.            (or ispell-current-dictionary "default"))))))
  3062.  
  3063.  
  3064. (defun ispell-begin-skip-region-regexp ()
  3065.   "Return a regexp of the search keys for region skipping.
  3066. Includes `ispell-skip-region-alist' plus tex, tib, html, and comment keys.
  3067. Must call after `ispell-buffer-local-parsing' due to dependence on mode."
  3068.   ;; start with regions generic to all buffers
  3069.   (let ((skip-regexp (ispell-begin-skip-region ispell-skip-region-alist)))
  3070.     ;; Comments
  3071.     (if (and (null ispell-check-comments) comment-start)
  3072.     (setq skip-regexp (concat (regexp-quote comment-start) "\\|"
  3073.                   skip-regexp)))
  3074.     (if (and (eq 'exclusive ispell-check-comments) comment-start)
  3075.     ;; search from end of current comment to start of next comment.
  3076.     (setq skip-regexp (concat (if (string= "" comment-end) "^"
  3077.                     (regexp-quote comment-end))
  3078.                   "\\|" skip-regexp)))
  3079.     ;; tib
  3080.     (if ispell-skip-tib
  3081.     (setq skip-regexp (concat ispell-tib-ref-beginning "\\|" skip-regexp)))
  3082.     ;; html stuff
  3083.     (if ispell-skip-html
  3084.     (setq skip-regexp (concat
  3085.                (ispell-begin-skip-region ispell-html-skip-alists)
  3086.                "\\|"
  3087.                skip-regexp)))
  3088.     ;; tex
  3089.     (if (eq ispell-parser 'tex)
  3090.     (setq skip-regexp (concat (ispell-begin-tex-skip-regexp) "\\|"
  3091.                   skip-regexp)))
  3092.     ;; messages
  3093.     (if (and ispell-checking-message
  3094.          (not (eq t ispell-checking-message)))
  3095.     (setq skip-regexp (concat
  3096.                (mapconcat (lambda (lst) (car lst))
  3097.                       ispell-checking-message
  3098.                       "\\|")
  3099.                "\\|"
  3100.                skip-regexp)))
  3101.  
  3102.     ;; return new regexp
  3103.     skip-regexp))
  3104.  
  3105.  
  3106. (defun ispell-begin-skip-region (skip-alist)
  3107.   "Regular expression for start of regions to skip generated from SKIP-ALIST.
  3108. Each selection should be a key of SKIP-ALIST;
  3109. otherwise, the current line is skipped."
  3110.   (mapconcat (lambda (lst) (if (stringp (car lst)) (car lst) (eval (car lst))))
  3111.          skip-alist
  3112.          "\\|"))
  3113.  
  3114.  
  3115. (defun ispell-begin-tex-skip-regexp ()
  3116.   "Regular expression of tex commands to skip.
  3117. Generated from `ispell-tex-skip-alists'."
  3118.   (concat
  3119.    ;; raw tex keys
  3120.    (mapconcat (function (lambda (lst) (car lst)))
  3121.           (car ispell-tex-skip-alists)
  3122.           "\\|")
  3123.    "\\|"
  3124.    ;; keys wrapped in begin{}
  3125.    (mapconcat (function (lambda (lst)
  3126.               (concat "\\\\begin[ \t\n]*{[ \t\n]*"
  3127.                   (car lst)
  3128.                   "[ \t\n]*}")))
  3129.           (car (cdr ispell-tex-skip-alists))
  3130.           "\\|")))
  3131.  
  3132.  
  3133. (defun ispell-skip-region-list ()
  3134.   "Return a list describing key and body regions to skip for this buffer.
  3135. Includes regions defined by `ispell-skip-region-alist', tex mode,
  3136. `ispell-html-skip-alists', and `ispell-checking-message'.
  3137. Manual checking must include comments and tib references.
  3138. The list is of the form described by variable `ispell-skip-region-alist'.
  3139. Must call after `ispell-buffer-local-parsing' due to dependence on mode."
  3140.   (let ((skip-alist ispell-skip-region-alist))
  3141.     ;; only additional explicit region definition is tex.
  3142.     (if (eq ispell-parser 'tex)
  3143.     (setq case-fold-search nil
  3144.           skip-alist (append (car ispell-tex-skip-alists)
  3145.                  (car (cdr ispell-tex-skip-alists))
  3146.                  skip-alist)))
  3147.     (if ispell-skip-html
  3148.     (setq skip-alist (append ispell-html-skip-alists skip-alist)))
  3149.     (if (and ispell-checking-message
  3150.          (not (eq t ispell-checking-message)))
  3151.     (setq skip-alist (append ispell-checking-message skip-alist)))
  3152.     skip-alist))
  3153.  
  3154.  
  3155. (defun ispell-tex-arg-end (&optional arg)
  3156.   "Skip across ARG number of braces."
  3157.   (condition-case nil
  3158.       (progn
  3159.     (while (looking-at "[ \t\n]*\\[") (forward-sexp))
  3160.     (forward-sexp (or arg 1)))
  3161.     (error
  3162.      (message "error skipping s-expressions at point %d." (point))
  3163.      (beep)
  3164.      (sit-for 2))))
  3165.  
  3166.  
  3167. (defun ispell-ignore-fcc (start end)
  3168.   "Delete the Fcc: message header when large attachments are included.
  3169. Return value `nil' if file with large attachments are saved.
  3170. This can be used to avoid multiple questions for multiple large attachments.
  3171. Returns point to starting location afterwards."
  3172.   (let ((result t))
  3173.     (if (and ispell-checking-message ispell-message-fcc-skip)
  3174.     (if (< ispell-message-fcc-skip (- end start))
  3175.         (let (case-fold-search head-end)
  3176.           (goto-char (point-min))
  3177.           (setq head-end
  3178.             (or (re-search-forward
  3179.              (concat "^" (regexp-quote mail-header-separator) "$")
  3180.              nil t)
  3181.             (re-search-forward "^$" nil t)
  3182.             (point-min)))
  3183.           (goto-char (point-min))
  3184.           (if (re-search-forward "^Fcc:" head-end t)
  3185.           (if (y-or-n-p
  3186.                "Save copy of this message with large attachments? ")
  3187.               (setq result nil)
  3188.             (beginning-of-line)
  3189.             (kill-line 1)))
  3190.           (goto-char end))))
  3191.     result))
  3192.  
  3193.  
  3194. (defun ispell-skip-region (key)
  3195.   "Skip across KEY and then to end of region.
  3196. Key lookup determines region to skip.
  3197. Point is placed at end of skipped region."
  3198.   ;; move over key to begin checking.
  3199.   (forward-char (length key))
  3200.   (let ((start (point))
  3201.     ;; Regenerate each call... This function can change region definition.
  3202.     (alist (ispell-skip-region-list))
  3203.     alist-key null-skip)
  3204.     (cond
  3205.      ;; what about quoted comment, or comment inside strings?
  3206.      ((and (null ispell-check-comments) comment-start
  3207.        (string= key comment-start))
  3208.       (if (string= "" comment-end)
  3209.       (forward-line)
  3210.     (search-forward comment-end ispell-region-end t)))
  3211.      ((and (eq 'exclusive ispell-check-comments) comment-start
  3212.        (string= key comment-end))
  3213.       (search-forward comment-start ispell-region-end :end))
  3214.      ((and ispell-skip-tib (string-match ispell-tib-ref-beginning key))
  3215.       (re-search-forward ispell-tib-ref-end ispell-region-end t))
  3216.      ;; markings from alist
  3217.      (t
  3218.       (while alist
  3219.     (setq alist-key (eval (car (car alist))))
  3220.     (if (string-match alist-key key)
  3221.         (progn
  3222.           (setq alist (cdr (car alist)))
  3223.           (cond
  3224.            ((null alist) (setq null-skip t)) ; done!  Just skip key.
  3225.            ((not (consp alist))
  3226.         ;; Search past end of spell region to find this region end.
  3227.         (re-search-forward (eval alist) (point-max) t))
  3228.            ((and (= 1 (length alist))
  3229.              (stringp (car alist)))
  3230.         (re-search-forward (car alist) (point-max) t))
  3231.            (t
  3232.         (setq null-skip t)    ; error handling in functions!
  3233.         (if (consp (cdr alist))
  3234.             (apply (car alist) (cdr alist))
  3235.           (funcall (car alist)))))
  3236.           (setq alist nil))
  3237.       (setq alist (cdr alist))))))
  3238.     (if (and (= start (point)) (null null-skip))
  3239.     (progn
  3240.       (message "Matching region end for `%s' point %d not found"
  3241.            key (point))
  3242.       (beep)
  3243.       (sit-for 2)))))
  3244.  
  3245.  
  3246. ;;; Grab the next line of data.
  3247. ;;; Returns a string with the line data
  3248. (defun ispell-get-line (start end in-comment)
  3249.   (let ((ispell-casechars (ispell-get-casechars))
  3250.     string)
  3251.     (cond                ; LOOK AT THIS LINE AND SKIP OR PROCESS
  3252.      ((eolp)                ; END OF LINE, just go to next line.
  3253.       (forward-line))
  3254.      ;;((looking-at "[-#@*+!%~^]")    ; SKIP SPECIAL ISPELL CHARACTERS
  3255.      ;; (forward-char 1))        ; not needed as quoted below.
  3256.      ((or (re-search-forward ispell-casechars end t) ; TEXT EXISTS
  3257.       (re-search-forward "[][()${}]" end t)) ; or MATH COMMANDS
  3258.       (setq string (concat "^" in-comment
  3259.                (buffer-substring-no-properties start end)
  3260.                "\n"))
  3261.       (goto-char end))
  3262.      (t (goto-char end)))        ; EMPTY LINE, skip it.
  3263.     string))
  3264.  
  3265.  
  3266. (defun ispell-looking-at (string)
  3267.   (let ((coding (ispell-get-coding-system))
  3268.     (len (length string)))
  3269.     (and (<= (+ (point) len) (point-max))
  3270.      (equal (encode-coding-string string coding)
  3271.         (encode-coding-string (buffer-substring-no-properties
  3272.                        (point) (+ (point) len))
  3273.                       coding)))))
  3274.  
  3275. ;;; Avoid error messages when compiling for these dynamic variables.
  3276. (defvar start)
  3277. (defvar end)
  3278.  
  3279. (defun ispell-process-line (string shift)
  3280.   "Send STRING, a line of text, to ispell and processes the result.
  3281. This will modify the buffer for spelling errors.
  3282. Requires variables START and END to be defined in its lexical scope.
  3283. Returns the sum SHIFT due to changes in word replacements."
  3284.   ;;(declare special start end)
  3285.   (let (poss accept-list)
  3286.     (if (not (numberp shift))
  3287.     (setq shift 0))
  3288.     ;; send string to spell process and get input.
  3289.     (ispell-send-string string)
  3290.     (while (progn
  3291.          (ispell-accept-output)
  3292.          ;; Last item of output contains a blank line.
  3293.          (not (string= "" (car ispell-filter)))))
  3294.     ;; parse all inputs from the stream one word at a time.
  3295.     ;; Place in FIFO order and remove the blank item.
  3296.     (setq ispell-filter (nreverse (cdr ispell-filter)))
  3297.     (while (and (not ispell-quit) ispell-filter)
  3298.       ;; get next word, accounting for accepted words and start shifts
  3299.       (setq poss (ispell-parse-output (car ispell-filter)
  3300.                       accept-list shift))
  3301.       (if (and poss (listp poss))    ; spelling error occurred.
  3302.       ;; Whenever we have misspellings, we can change
  3303.       ;; the buffer.  Keep boundaries as markers.
  3304.       ;; Markers can move with highlighting!  This destroys
  3305.       ;; end of region markers line-end and ispell-region-end
  3306.       (let ((word-start
  3307.          (copy-marker (+ start ispell-offset (car (cdr poss)))))
  3308.         (word-len (length (car poss)))
  3309.         (line-end (copy-marker end))
  3310.         (line-start (copy-marker start))
  3311.         recheck-region replace)
  3312.         (goto-char word-start)
  3313.         ;; Adjust the horizontal scroll & point
  3314.         (ispell-horiz-scroll)
  3315.         (goto-char (+ word-len word-start))
  3316.         (ispell-horiz-scroll)
  3317.         (goto-char word-start)
  3318.         (ispell-horiz-scroll)
  3319.  
  3320.         ;; Alignment cannot be tracked and this error will occur when
  3321.         ;; `query-replace' makes multiple corrections on the starting line.
  3322.         (or (ispell-looking-at (car poss))
  3323.         ;; This occurs due to filter pipe problems
  3324.         (error (concat "Ispell misalignment: word "
  3325.                    "`%s' point %d; probably incompatible versions")
  3326.                (car poss) (marker-position word-start)))
  3327.         ;; ispell-cmd-loop can go recursive & change buffer
  3328.         (if ispell-keep-choices-win
  3329.         (setq replace (ispell-command-loop
  3330.                    (car (cdr (cdr poss)))
  3331.                    (car (cdr (cdr (cdr poss))))
  3332.                    (car poss) (marker-position word-start)
  3333.                    (+ word-len (marker-position word-start))))
  3334.           (save-window-excursion
  3335.         (setq replace (ispell-command-loop
  3336.                    (car (cdr (cdr poss)))
  3337.                    (car (cdr (cdr (cdr poss))))
  3338.                    (car poss) (marker-position word-start)
  3339.                    (+ word-len (marker-position word-start))))))
  3340.  
  3341.         (goto-char word-start)
  3342.         ;; Recheck when query replace edit changes misspelled word.
  3343.         ;; Error in tex mode when a potential math mode change exists.
  3344.         (if (and replace (listp replace) (= 2 (length replace)))
  3345.         (if (and (eq ispell-parser 'tex)
  3346.              (string-match "[\\\\][]()[]\\|\\\\begin\\|\\$"
  3347.                        (regexp-quote string)))
  3348.             (error
  3349.              "Don't start query replace on a line with math characters"
  3350.              )
  3351.           (set-marker line-end (point))
  3352.           (setq ispell-filter nil
  3353.             recheck-region t)))
  3354.  
  3355.         ;; insert correction if needed
  3356.         (cond
  3357.          ((or (null replace)
  3358.           (equal 0 replace))    ; ACCEPT/INSERT
  3359.           (if (equal 0 replace)    ; BUFFER-LOCAL DICT ADD
  3360.           (ispell-add-per-file-word-list (car poss)))
  3361.           ;; do not recheck accepted word on this line
  3362.           (setq accept-list (cons (car poss) accept-list)))
  3363.          (t                ; replacement word selected or entered
  3364.           (delete-region (point) (+ word-len (point)))
  3365.           (if (not (listp replace))
  3366.           (progn
  3367.             (ispell-insert-word replace) ; insert dictionary word
  3368.             (ispell-send-replacement (car poss) replace)
  3369.             (setq accept-list (cons replace accept-list)))
  3370.         (let ((replace-word (car replace)))
  3371.           ;; Recheck hand entered replacement word
  3372.           (insert replace-word)
  3373.           (ispell-send-replacement (car poss) replace-word)
  3374.           (if (car (cdr replace))
  3375.               (save-window-excursion
  3376.             (delete-other-windows) ; to correctly show help.
  3377.             ;; Assume case-replace &
  3378.             ;; case-fold-search correct?
  3379.             (query-replace (car poss) (car replace) t)))
  3380.           (goto-char word-start)
  3381.           ;; do not recheck if already accepted
  3382.           (if (member replace-word accept-list)
  3383.               (setq accept-list (cons replace-word accept-list)
  3384.                 replace replace-word)
  3385.             (let ((region-end (copy-marker ispell-region-end)))
  3386.               (setq recheck-region ispell-filter
  3387.                 ispell-filter nil ; save filter
  3388.                 shift 0    ; already accounted
  3389.                 shift (ispell-region
  3390.                   word-start
  3391.                   (+ word-start (length replace-word))
  3392.                   t shift))
  3393.               (if (null shift)    ; quitting check.
  3394.               (setq shift 0))
  3395.               (set-marker ispell-region-end region-end)
  3396.               (set-marker region-end nil)
  3397.               (setq ispell-filter recheck-region
  3398.                 recheck-region nil
  3399.                 replace replace-word)))))
  3400.  
  3401.           (setq shift (+ shift (- (length replace) word-len)))
  3402.  
  3403.           ;; Move line-start across word...
  3404.           ;; new shift function does this now...
  3405.           ;;(set-marker line-start (+ line-start
  3406.           ;;            (- (length replace)
  3407.           ;;               (length (car poss)))))
  3408.           ))
  3409.         (if (not ispell-quit)
  3410.         (let (message-log-max)
  3411.           (message "Continuing spelling check using %s with %s dictionary..."
  3412.                (file-name-nondirectory ispell-program-name)
  3413.                (or ispell-current-dictionary "default"))))
  3414.         (sit-for 0)
  3415.         (setq start (marker-position line-start)
  3416.           end (marker-position line-end))
  3417.         ;; Adjust markers when end of region lost from highlighting.
  3418.         (if (and (not recheck-region) (< end (+ word-start word-len)))
  3419.         (setq end (+ word-start word-len)))
  3420.         (if (= word-start ispell-region-end)
  3421.         (set-marker ispell-region-end (+ word-start word-len)))
  3422.         ;; going out of scope - unneeded
  3423.         (set-marker line-start nil)
  3424.         (set-marker word-start nil)
  3425.         (set-marker line-end nil)))
  3426.       ;; finished with misspelling!
  3427.       (setq ispell-filter (cdr ispell-filter)))
  3428.     shift))
  3429.  
  3430.  
  3431. ;;;###autoload
  3432. (defun ispell-comments-and-strings ()
  3433.   "Check comments and strings in the current buffer for spelling errors."
  3434.   (interactive)
  3435.   (goto-char (point-min))
  3436.   (let (state done)
  3437.     (while (not done)
  3438.       (setq done t)
  3439.       (setq state (parse-partial-sexp (point) (point-max)
  3440.                       nil nil state 'syntax-table))
  3441.       (if (or (nth 3 state) (nth 4 state))
  3442.       (let ((start (point)))
  3443.         (setq state (parse-partial-sexp start (point-max)
  3444.                         nil nil state 'syntax-table))
  3445.         (if (or (nth 3 state) (nth 4 state))
  3446.         (error "Unterminated string or comment"))
  3447.         (save-excursion
  3448.           (setq done (not (ispell-region start (point))))))))))
  3449.  
  3450.  
  3451. ;;;###autoload
  3452. (defun ispell-buffer ()
  3453.   "Check the current buffer for spelling errors interactively."
  3454.   (interactive)
  3455.   (ispell-region (point-min) (point-max)))
  3456.  
  3457.  
  3458. ;;;###autoload
  3459. (defun ispell-continue ()
  3460.   "Continue a halted spelling session beginning with the current word."
  3461.   (interactive)
  3462.   (if (not (marker-position ispell-region-end))
  3463.       (message "No session to continue.  Use 'X' command when checking!")
  3464.     (if (not (equal (marker-buffer ispell-region-end) (current-buffer)))
  3465.     (message "Must continue ispell from buffer %s"
  3466.          (buffer-name (marker-buffer ispell-region-end)))
  3467.       (ispell-region
  3468.        ;; find beginning of current word:
  3469.        (car (cdr (ispell-get-word t)))
  3470.        (marker-position ispell-region-end)))))
  3471.  
  3472.  
  3473. ;;; Horizontal scrolling
  3474. (defun ispell-horiz-scroll ()
  3475.   "Place point within the horizontal visibility of its window area."
  3476.   (if truncate-lines            ; display truncating lines?
  3477.       ;; See if display needs to be scrolled.
  3478.       (let ((column (- (current-column) (max (window-hscroll) 1))))
  3479.     (if (and (< column 0) (> (window-hscroll) 0))
  3480.         (scroll-right (max (- column) 10))
  3481.       (if (>= column (- (window-width) 2))
  3482.           (scroll-left (max (- column (window-width) -3) 10)))))))
  3483.  
  3484.  
  3485. ;;; Interactive word completion.
  3486. ;;; Forces "previous-word" processing.  Do we want to make this selectable?
  3487.  
  3488. ;;;###autoload
  3489. (defun ispell-complete-word (&optional interior-frag)
  3490.   "Try to complete the word before or under point (see `lookup-words').
  3491. If optional INTERIOR-FRAG is non-nil then the word may be a character
  3492. sequence inside of a word.
  3493.  
  3494. Standard ispell choices are then available."
  3495.   (interactive "P")
  3496.   (let ((cursor-location (point))
  3497.     (case-fold-search-val case-fold-search)
  3498.     (word (ispell-get-word nil "\\*")) ; force "previous-word" processing.
  3499.     start end possibilities replacement)
  3500.     (setq start (car (cdr word))
  3501.       end (car (cdr (cdr word)))
  3502.       word (car word)
  3503.       possibilities
  3504.       (or (string= word "")        ; Will give you every word
  3505.           (lookup-words (concat (and interior-frag "*") word
  3506.                     (if (or interior-frag (null ispell-look-p))
  3507.                     "*"))
  3508.                 ispell-complete-word-dict)))
  3509.     (cond ((eq possibilities t)
  3510.        (message "No word to complete"))
  3511.       ((null possibilities)
  3512.        (message "No match for \"%s\"" word))
  3513.       (t                ; There is a modification...
  3514.        (setq case-fold-search nil)    ; Try and respect case of word.
  3515.        (cond
  3516.         ((string-equal (upcase word) word)
  3517.          (setq possibilities (mapcar 'upcase possibilities)))
  3518.         ((eq (upcase (aref word 0)) (aref word 0))
  3519.              (setq possibilities (mapcar (function
  3520.                                           (lambda (pos)
  3521.                                             (if (eq (aref word 0) (aref pos 0))
  3522.                         pos
  3523.                                               (capitalize pos))))
  3524.                                          possibilities))))
  3525.        (setq case-fold-search case-fold-search-val)
  3526.        (save-window-excursion
  3527.          (setq replacement
  3528.            (ispell-command-loop possibilities nil word start end)))
  3529.        (cond
  3530.         ((equal 0 replacement)    ; BUFFER-LOCAL ADDITION
  3531.          (ispell-add-per-file-word-list word))
  3532.         (replacement        ; REPLACEMENT WORD
  3533.          (delete-region start end)
  3534.          (setq word (if (atom replacement) replacement (car replacement))
  3535.            cursor-location (+ (- (length word) (- end start))
  3536.                       cursor-location))
  3537.          (ispell-insert-word word)
  3538.          (if (not (atom replacement)) ; recheck spelling of replacement.
  3539.          (progn
  3540.            (goto-char cursor-location)
  3541.            (ispell-word nil t)))))
  3542.        (if (get-buffer ispell-choices-buffer)
  3543.            (kill-buffer ispell-choices-buffer))))
  3544.     (ispell-pdict-save ispell-silently-savep)
  3545.     (goto-char cursor-location)))
  3546.  
  3547.  
  3548. ;;;###autoload
  3549. (defun ispell-complete-word-interior-frag ()
  3550.   "Completes word matching character sequence inside a word."
  3551.   (interactive)
  3552.   (ispell-complete-word t))
  3553.  
  3554.  
  3555. ;;;###autoload
  3556. (defun ispell ()
  3557.   "Interactively check a region or buffer for spelling errors.
  3558. If `transient-mark-mode' is on, and a region is active, spell-check
  3559. that region.  Otherwise spell-check the buffer.
  3560.  
  3561. Ispell dictionaries are not distributed with Emacs.  If you are
  3562. looking for a dictionary, please see the distribution of the GNU ispell
  3563. program, or do an Internet search; there are various dictionaries
  3564. available on the net."
  3565.   (interactive)
  3566.   (if (and (boundp 'transient-mark-mode) transient-mark-mode
  3567.        (boundp 'mark-active) mark-active)
  3568.       (ispell-region (region-beginning) (region-end))
  3569.     (ispell-buffer)))
  3570.  
  3571.  
  3572. ;;; **********************************************************************
  3573. ;;;             Ispell Minor Mode
  3574. ;;; **********************************************************************
  3575.  
  3576. (defvar ispell-minor-mode nil
  3577.   "Non-nil if Ispell minor mode is enabled.")
  3578. ;; Variable indicating that ispell minor mode is active.
  3579. (make-variable-buffer-local 'ispell-minor-mode)
  3580.  
  3581. (or (assq 'ispell-minor-mode minor-mode-alist)
  3582.     (setq minor-mode-alist
  3583.           (cons '(ispell-minor-mode " Spell") minor-mode-alist)))
  3584.  
  3585. (defvar ispell-minor-keymap
  3586.   (let ((map (make-sparse-keymap)))
  3587.     (define-key map " " 'ispell-minor-check)
  3588.     (define-key map "\r" 'ispell-minor-check)
  3589.     map)
  3590.   "Keymap used for Ispell minor mode.")
  3591.  
  3592. (or (not (boundp 'minor-mode-map-alist))
  3593.     (assoc 'ispell-minor-mode minor-mode-map-alist)
  3594.     (setq minor-mode-map-alist
  3595.           (cons (cons 'ispell-minor-mode ispell-minor-keymap)
  3596.                 minor-mode-map-alist)))
  3597.  
  3598. ;;;###autoload
  3599. (defun ispell-minor-mode (&optional arg)
  3600.   "Toggle Ispell minor mode.
  3601. With prefix argument ARG, turn Ispell minor mode on if ARG is positive,
  3602. otherwise turn it off.
  3603.  
  3604. In Ispell minor mode, pressing SPC or RET
  3605. warns you if the previous word is incorrectly spelled.
  3606.  
  3607. All the buffer-local variables and dictionaries are ignored -- to read
  3608. them into the running ispell process, type \\[ispell-word] SPC."
  3609.   (interactive "P")
  3610.   (setq ispell-minor-mode
  3611.     (not (or (and (null arg) ispell-minor-mode)
  3612.          (<= (prefix-numeric-value arg) 0))))
  3613.   (force-mode-line-update))
  3614.  
  3615. (defun ispell-minor-check ()
  3616.   "Check previous word then continue with the normal binding of this key.
  3617. Don't check previous word when character before point is a space or newline.
  3618. Don't read buffer-local settings or word lists."
  3619.   (interactive "*")
  3620.   (let ((ispell-minor-mode nil)
  3621.     (ispell-check-only t)
  3622.     (last-char (char-after (1- (point)))))
  3623.     (command-execute (key-binding (this-command-keys)))
  3624.     (if (not (or (eq last-char ?\ ) (eq last-char ?\n)
  3625.          (and ispell-skip-html (eq last-char ?>))
  3626.          (and ispell-skip-html (eq last-char ?\;))))
  3627.     (ispell-word nil t))))
  3628.  
  3629.  
  3630. ;;; **********************************************************************
  3631. ;;;             Ispell Message
  3632. ;;; **********************************************************************
  3633.  
  3634. (defvar ispell-message-text-end
  3635.   (mapconcat (function identity)
  3636.          '(
  3637.            ;; Don't spell check signatures
  3638.            "^-- $"
  3639.            ;; Matches postscript files.
  3640.            ;;"^%!PS-Adobe-[123].0"
  3641.            ;; Matches uuencoded text
  3642.            ;;"^begin [0-9][0-9][0-9] .*\nM.*\nM.*\nM"
  3643.            ;; Matches shell files (especially auto-decoding)
  3644.            "^#! /bin/[ck]?sh"
  3645.            ;; Matches context difference listing
  3646.            "\\(\\(^cd .*\n\\)?diff -c .*\\)?\n\\*\\*\\* .*\n--- .*\n\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*"
  3647.            ;; Matches unidiff difference listing
  3648.            "\\(diff -u .*\\)?\n--- .*\n\\+\\+\\+ .*\n@@ [-+][0-9]+,[0-9]+ [-+][0-9]+,[0-9]+ @@\n"
  3649.            ;; Matches reporter.el bug report
  3650.            "^current state:\n==============\n"
  3651.            ;; Matches commonly used "cut" boundaries
  3652.            "^\\(- \\)?[-=_]+\\s ?\\(cut here\\|Environment Follows\\)")
  3653.          "\\|")
  3654.   "*End of text which will be checked in `ispell-message'.
  3655. If it is a string, limit at first occurrence of that regular expression.
  3656. Otherwise, it must be a function which is called to get the limit.")
  3657. (put 'ispell-message-text-end 'risky-local-variable t)
  3658.  
  3659.  
  3660. (defun ispell-mime-multipartp (&optional limit)
  3661.   "Return multipart message start boundary or nil if none."
  3662.   ;; caller must ensure `case-fold-search' is set to `t'
  3663.   (and
  3664.    (re-search-forward
  3665.     "Content-Type: *multipart/\\([^ \t\n]*;[ \t]*[\n]?[ \t]*\\)+boundary="
  3666.     limit t)
  3667.    (let (boundary)
  3668.      (if (looking-at "\"")
  3669.      (let (start)
  3670.        (forward-char)
  3671.        (setq start (point))
  3672.        (while (not (looking-at "\""))
  3673.          (forward-char 1))
  3674.        (setq boundary (buffer-substring-no-properties start (point))))
  3675.        (let ((start (point)))
  3676.      (while (looking-at "[-0-9a-zA-Z'()+_,./:=?]")
  3677.        (forward-char))
  3678.      (setq boundary (buffer-substring-no-properties start (point)))))
  3679.      (if (< (length boundary) 1)
  3680.      (setq boundary nil)
  3681.        (concat "--" boundary)))))
  3682.  
  3683.  
  3684. (defun ispell-mime-skip-part (boundary)
  3685.   "Move point across header, or entire MIME part if message is encoded.
  3686. All specified types except `7bit' `8bit' and `quoted-printable' are considered
  3687. encoded and therefore skipped.  See rfc 1521, 2183, ...
  3688. If no boundary is given, then entire message is skipped.
  3689.  
  3690. This starts one line ABOVE the MIME content messages, on the boundary marker,
  3691. for operation with the generic region-skipping code.
  3692. This places new MIME boundaries into variable `ispell-checking-message'."
  3693.   (forward-line)            ; skip over boundary to headers
  3694.   (let ((save-case-fold-search case-fold-search)
  3695.     (continuep t)
  3696.     textp)
  3697.     (setq case-fold-search t
  3698.       ispell-skip-html nil)
  3699.     (while continuep
  3700.       (setq continuep nil)
  3701.       (if (looking-at "Content-Type: *text/")
  3702.       (progn
  3703.         (goto-char (match-end 0))
  3704.         (if (looking-at "html")
  3705.         (setq ispell-skip-html t))
  3706.         (setq textp t
  3707.           continuep t)
  3708.         (re-search-forward "\\(.*;[ \t]*[\n]\\)*.*$" nil t)
  3709.         (forward-line)))
  3710.       (if (looking-at "Content-Transfer-Encoding: *\\([^ \t\n]*\\)")
  3711.       (let ((match (buffer-substring (match-beginning 1) (match-end 1))))
  3712.         (setq textp (member (upcase match)
  3713.                 ;; only spell check the following encodings:
  3714.                 '("7BIT" "8BIT" "QUOTED-PRINTABLE" "BINARY"))
  3715.           continuep t)
  3716.         (goto-char (match-end 0))
  3717.         (re-search-forward "\\(.*;[ \t]*[\n]\\)*.*$" nil t)
  3718.         (forward-line)))
  3719.       ;; hierarchical boundary definition
  3720.       (if (looking-at "Content-Type: *multipart/")
  3721.       (let ((new-boundary (ispell-mime-multipartp)))
  3722.         (if (string-match new-boundary boundary)
  3723.         (setq continuep t)
  3724.           ;; first pass redefine skip function to include new boundary
  3725.           ;;(re-search-backward boundary nil t)
  3726.           (forward-line)
  3727.           (setq ispell-checking-message
  3728.             (cons
  3729.              (list new-boundary 'ispell-mime-skip-part new-boundary)
  3730.              (if (eq t ispell-checking-message) nil
  3731.                ispell-checking-message))
  3732.             textp t
  3733.             continuep t)))
  3734.     ;; Skip all MIME headers that don't affect spelling
  3735.     (if (looking-at "Content-[^ \t]*: *\\(.*;[ \t]*[\n]\\)*.*$")
  3736.         (progn
  3737.           (setq continuep t)
  3738.           (goto-char (match-end 0))
  3739.           (forward-line)))))
  3740.  
  3741.     (setq case-fold-search save-case-fold-search)
  3742.     (if textp
  3743.     (point)
  3744.       ;; encoded message.  Skip to boundary, or entire message.
  3745.       (if (not boundary)
  3746.       (goto-char (point-max))
  3747.     (re-search-forward boundary nil t)
  3748.     (beginning-of-line)
  3749.     (point)))))
  3750.  
  3751.  
  3752. ;;;###autoload
  3753. (defun ispell-message ()
  3754.   "Check the spelling of a mail message or news post.
  3755. Don't check spelling of message headers except the Subject field.
  3756. Don't check included messages.
  3757.  
  3758. To abort spell checking of a message region and send the message anyway,
  3759. use the `x' command.  (Any subsequent regions will be checked.)
  3760. The `X' command aborts the message send so that you can edit the buffer.
  3761.  
  3762. To spell-check whenever a message is sent, include the appropriate lines
  3763. in your .emacs file:
  3764.    (add-hook 'message-send-hook 'ispell-message)  ;; GNUS 5
  3765.    (add-hook 'news-inews-hook 'ispell-message)    ;; GNUS 4
  3766.    (add-hook 'mail-send-hook  'ispell-message)
  3767.    (add-hook 'mh-before-send-letter-hook 'ispell-message)
  3768.  
  3769. You can bind this to the key C-c i in GNUS or mail by adding to
  3770. `news-reply-mode-hook' or `mail-mode-hook' the following lambda expression:
  3771.    (function (lambda () (local-set-key \"\\C-ci\" 'ispell-message)))"
  3772.   (interactive)
  3773.   (save-excursion
  3774.     (goto-char (point-min))
  3775.     (let* (boundary mimep
  3776.        (ispell-skip-region-alist-save ispell-skip-region-alist)
  3777.        ;; Nil when message came from outside (eg calling Emacs as editor)
  3778.        ;; Non-nil marker of end of headers.
  3779.        (internal-messagep
  3780.         (re-search-forward
  3781.          (concat "^" (regexp-quote mail-header-separator) "$") nil t))
  3782.        (end-of-headers        ; Start of body.
  3783.         (copy-marker
  3784.          (or internal-messagep
  3785.          (re-search-forward "^$" nil t)
  3786.          (point-min))))
  3787.        (limit (copy-marker        ; End of region we will spell check.
  3788.            (cond
  3789.             ((not ispell-message-text-end) (point-max))
  3790.             ((char-or-string-p ispell-message-text-end)
  3791.              (if (re-search-forward ispell-message-text-end nil t)
  3792.              (match-beginning 0)
  3793.                (point-max)))
  3794.             (t (min (point-max) (funcall ispell-message-text-end))))))
  3795.        (default-prefix   ; Vanilla cite prefix (just used for cite-regexp)
  3796.          (if (and (boundp 'mail-yank-prefix) mail-yank-prefix)
  3797.          (ispell-non-empty-string mail-yank-prefix)
  3798.            "   \\|\t"))
  3799.        (cite-regexp            ;Prefix of quoted text
  3800.         (cond
  3801.          ((functionp 'sc-cite-regexp)    ; sc 3.0
  3802.           (ispell-with-no-warnings
  3803.         (concat "\\(" (sc-cite-regexp) "\\)" "\\|"
  3804.             (ispell-non-empty-string sc-reference-tag-string))))
  3805.          ((boundp 'sc-cite-regexp)        ; sc 2.3
  3806.           (concat "\\(" sc-cite-regexp "\\)" "\\|"
  3807.               (ispell-with-no-warnings
  3808.                (ispell-non-empty-string sc-reference-tag-string))))
  3809.          ((or (equal major-mode 'news-reply-mode) ;GNUS 4 & below
  3810.           (equal major-mode 'message-mode))   ;GNUS 5
  3811.           (concat "In article <" "\\|"
  3812.               "[^,;&+=\n]+ <[^,;&+=]+> writes:" "\\|"
  3813.               (ispell-with-no-warnings message-cite-prefix-regexp)
  3814.               "\\|"
  3815.               default-prefix))
  3816.          ((equal major-mode 'mh-letter-mode) ; mh mail message
  3817.           (concat "[^,;&+=\n]+ writes:" "\\|"
  3818.               (ispell-with-no-warnings
  3819.                (ispell-non-empty-string mh-ins-buf-prefix))))
  3820.          ((not internal-messagep)    ; Assume nn sent us this message.
  3821.           (concat "In [a-zA-Z.]+ you write:" "\\|"
  3822.               "In <[^,;&+=]+> [^,;&+=]+ writes:" "\\|"
  3823.               " *> *"))
  3824.          ((boundp 'vm-included-text-prefix) ; VM mail message
  3825.           (concat "[^,;&+=\n]+ writes:" "\\|"
  3826.               (ispell-non-empty-string vm-included-text-prefix)))
  3827.          (t default-prefix)))
  3828.        (ispell-skip-region-alist
  3829.         (cons (list (concat "^\\(" cite-regexp "\\)")
  3830.             (function forward-line))
  3831.           ispell-skip-region-alist))
  3832.        (old-case-fold-search case-fold-search)
  3833.        (dictionary-alist ispell-message-dictionary-alist)
  3834.        (ispell-checking-message t))
  3835.  
  3836.       ;; Select dictionary for message
  3837.       (or (local-variable-p 'ispell-local-dictionary (current-buffer))
  3838.       (while dictionary-alist
  3839.         (goto-char (point-min))
  3840.         (if (re-search-forward (car (car dictionary-alist))
  3841.                    end-of-headers t)
  3842.         (setq ispell-local-dictionary (cdr (car dictionary-alist))
  3843.               dictionary-alist nil)
  3844.           (setq dictionary-alist (cdr dictionary-alist)))))
  3845.  
  3846.       (unwind-protect
  3847.       (progn
  3848.         ;; Spell check any original Subject:
  3849.         (goto-char (point-min))
  3850.         (setq case-fold-search t
  3851.           mimep (re-search-forward "MIME-Version:" end-of-headers t))
  3852.         (goto-char (point-min))
  3853.         (if (re-search-forward "^Subject: *" end-of-headers t)
  3854.         (progn
  3855.           (goto-char (match-end 0))
  3856.           (if (and (not (looking-at ".*Re\\>"))
  3857.                (not (looking-at "\\[")))
  3858.               (progn
  3859.             (setq case-fold-search old-case-fold-search)
  3860.             (ispell-region (point)
  3861.                        (progn ;Tab-initiated continuation lns.
  3862.                      (end-of-line)
  3863.                      (while (looking-at "\n[ \t]")
  3864.                        (end-of-line 2))
  3865.                      (point)))))))
  3866.         (if mimep
  3867.         (progn
  3868.           (goto-char (point-min))
  3869.           (setq boundary (ispell-mime-multipartp end-of-headers))))
  3870.         ;; Adjust message limit to MIME message if necessary.
  3871.         (and boundary
  3872.          (re-search-forward (concat boundary "--") nil t)
  3873.          (re-search-backward boundary nil t)
  3874.          (< (point) (marker-position limit))
  3875.          (set-marker limit (point)))
  3876.         (goto-char (point-min))
  3877.         ;; Select type or skip checking if this is a non-multipart message
  3878.         ;; Point moved to end of buffer if region is encoded.
  3879.         (if (and mimep (not boundary))
  3880.         (let (skip-regexp)    ; protect from `ispell-mime-skip-part'
  3881.           (goto-char (point-min))
  3882.           (re-search-forward "Content-[^ \t]*:" end-of-headers t)
  3883.           (forward-line -1)    ; following fn starts one line above
  3884.           (ispell-mime-skip-part nil)
  3885.           ;; if message-text-end region, limit may be less than point.
  3886.           (if (> (point) limit)
  3887.               (set-marker limit (point)))))
  3888.         (goto-char (max end-of-headers (point)))
  3889.         (forward-line 1)
  3890.         (setq case-fold-search old-case-fold-search)
  3891.         ;; Define MIME regions to skip.
  3892.         (if boundary
  3893.         (setq ispell-checking-message
  3894.               (list (list boundary 'ispell-mime-skip-part boundary))))
  3895.         (ispell-region (point) limit))
  3896.     (set-marker end-of-headers nil)
  3897.     (set-marker limit nil)
  3898.     (setq ispell-skip-region-alist ispell-skip-region-alist-save
  3899.           ispell-skip-html nil
  3900.           case-fold-search old-case-fold-search)))))
  3901.  
  3902.  
  3903. (defun ispell-non-empty-string (string)
  3904.   (if (or (not string) (string-equal string ""))
  3905.       "\\'\\`" ; An unmatchable string if string is null.
  3906.     (regexp-quote string)))
  3907.  
  3908.  
  3909. ;;; **********************************************************************
  3910. ;;;             Buffer Local Functions
  3911. ;;; **********************************************************************
  3912.  
  3913.  
  3914. (defun ispell-accept-buffer-local-defs ()
  3915.   "Load all buffer-local information, restarting Ispell when necessary."
  3916.   (ispell-buffer-local-dict)        ; May kill ispell-process.
  3917.   (ispell-buffer-local-words)        ; Will initialize ispell-process.
  3918.   (ispell-buffer-local-parsing))
  3919.  
  3920.  
  3921. (defun ispell-buffer-local-parsing ()
  3922.   "Place Ispell into parsing mode for this buffer.
  3923. Overrides the default parsing mode.
  3924. Includes Latex/Nroff modes and extended character mode."
  3925.   ;; (ispell-init-process) must already be called.
  3926.   (ispell-send-string "!\n")        ; Put process in terse mode.
  3927.   ;; We assume all major modes with "tex-mode" in them should use latex parsing
  3928.   ;; When exclusively checking comments, set to raw text mode (nroff).
  3929.   (if (and (not (eq 'exclusive ispell-check-comments))
  3930.        (or (and (eq ispell-parser 'use-mode-name)
  3931.             (string-match "[Tt][Ee][Xx]-mode"
  3932.                   (symbol-name major-mode)))
  3933.            (eq ispell-parser 'tex)))
  3934.       (progn
  3935.     (ispell-send-string "+\n")    ; set ispell mode to tex
  3936.     (if (not (eq ispell-parser 'tex))
  3937.         (set (make-local-variable 'ispell-parser) 'tex)))
  3938.     (ispell-send-string "-\n"))        ; set mode to normal (nroff)
  3939.   ;; If needed, test for SGML & HTML modes and set a buffer local nil/t value.
  3940.   (if (and ispell-skip-html (not (eq ispell-skip-html t)))
  3941.       (setq ispell-skip-html
  3942.         (not (null (string-match "sgml\\|html\\|xml"
  3943.                      (downcase (symbol-name major-mode)))))))
  3944.   ;; Set default extended character mode for given buffer, if any.
  3945.   (let ((extended-char-mode (ispell-get-extended-character-mode)))
  3946.     (if extended-char-mode
  3947.     (ispell-send-string (concat extended-char-mode "\n"))))
  3948.   ;; Set buffer-local parsing mode and extended character mode, if specified.
  3949.   (save-excursion
  3950.     (goto-char (point-max))
  3951.     ;; Uses last occurrence of ispell-parsing-keyword
  3952.     (if (search-backward ispell-parsing-keyword nil t)
  3953.     (let ((end (save-excursion (end-of-line) (point)))
  3954.           string)
  3955.       (search-forward ispell-parsing-keyword)
  3956.       (while (re-search-forward " *\\([^ \"]+\\)" end t)
  3957.         ;; space separated definitions.
  3958.         (setq string (downcase (match-string-no-properties 1)))
  3959.         (cond ((and (string-match "latex-mode" string)
  3960.             (not (eq 'exclusive ispell-check-comments)))
  3961.            (ispell-send-string "+\n~tex\n"))
  3962.           ((string-match "nroff-mode" string)
  3963.            (ispell-send-string "-\n~nroff\n"))
  3964.           ((string-match "~" string) ; Set extended character mode.
  3965.            (ispell-send-string (concat string "\n")))
  3966.           (t (message "Invalid Ispell Parsing argument!")
  3967.              (sit-for 2))))))))
  3968.  
  3969.  
  3970. ;;; Can kill the current ispell process
  3971.  
  3972. (defun ispell-buffer-local-dict (&optional no-reload)
  3973.   "Initializes local dictionary and local personal dictionary.
  3974. If optional NO-RELOAD is non-nil, do not make any dictionary reloading.
  3975. When a dictionary is defined in the buffer (see variable
  3976. `ispell-dictionary-keyword'), it will override the local setting
  3977. from \\[ispell-change-dictionary].
  3978. Both should not be used to define a buffer-local dictionary."
  3979.   (save-excursion
  3980.     (goto-char (point-min))
  3981.     (let (end)
  3982.       ;; Override the local variable definition.
  3983.       ;; Uses last occurrence of ispell-dictionary-keyword.
  3984.       (goto-char (point-max))
  3985.       (unless ispell-local-dictionary-overridden
  3986.     (if (search-backward ispell-dictionary-keyword nil t)
  3987.         (progn
  3988.           (search-forward ispell-dictionary-keyword)
  3989.           (setq end (save-excursion (end-of-line) (point)))
  3990.           (if (re-search-forward " *\\([^ \"]+\\)" end t)
  3991.           (setq ispell-local-dictionary
  3992.             (match-string-no-properties 1))))))
  3993.       (goto-char (point-max))
  3994.       (if (search-backward ispell-pdict-keyword nil t)
  3995.       (progn
  3996.         (search-forward ispell-pdict-keyword)
  3997.         (setq end (save-excursion (end-of-line) (point)))
  3998.         (if (re-search-forward " *\\([^ \"]+\\)" end t)
  3999.         (setq ispell-local-pdict
  4000.               (match-string-no-properties 1)))))))
  4001.   (unless no-reload
  4002.     ;; Reload if new dictionary (maybe the personal one) defined.
  4003.     (ispell-internal-change-dictionary)))
  4004.  
  4005.  
  4006. (defun ispell-buffer-local-words ()
  4007.   "Load the buffer-local dictionary in the current buffer."
  4008.   ;; If there's an existing ispell process that's wrong for this use,
  4009.   ;; kill it.
  4010.   (if (and ispell-buffer-local-name
  4011.        (not (equal ispell-buffer-local-name (buffer-name))))
  4012.       (ispell-kill-ispell t))
  4013.   ;; Actually start a new ispell process, because we need
  4014.   ;; to send commands now to specify the local words to it.
  4015.   (ispell-init-process)
  4016.   (save-excursion
  4017.     (goto-char (point-min))
  4018.     (while (search-forward ispell-words-keyword nil t)
  4019.       (or ispell-buffer-local-name
  4020.       (setq ispell-buffer-local-name (buffer-name)))
  4021.       (let ((end (save-excursion (end-of-line) (point)))
  4022.         (ispell-casechars (ispell-get-casechars))
  4023.         string)
  4024.     ;; buffer-local words separated by a space, and can contain
  4025.     ;; any character other than a space.  Not rigorous enough.
  4026.     (while (re-search-forward " *\\([^ ]+\\)" end t)
  4027.       (setq string (match-string-no-properties 1))
  4028.       ;; This can fail when string contains a word with invalid chars.
  4029.       ;; Error handling needs to be added between ispell and Emacs.
  4030.       (if (and (< 1 (length string))
  4031.            (equal 0 (string-match ispell-casechars string)))
  4032.           (ispell-send-string (concat "@" string "\n"))))))))
  4033.  
  4034.  
  4035. ;;; returns optionally adjusted region-end-point.
  4036.  
  4037. (defun ispell-add-per-file-word-list (word)
  4038.   "Add WORD to the per-file word list."
  4039.   (or ispell-buffer-local-name
  4040.       (setq ispell-buffer-local-name (buffer-name)))
  4041.   (save-excursion
  4042.     (goto-char (point-min))
  4043.     (let ((old-case-fold-search case-fold-search)
  4044.       line-okay search done found)
  4045.       (while (not done)
  4046.     (setq case-fold-search nil
  4047.           search (search-forward ispell-words-keyword nil 'move)
  4048.           found (or found search)
  4049.           line-okay (< (+ (length word) 1 ; 1 for space after word..
  4050.                   (progn (end-of-line) (current-column)))
  4051.                80)
  4052.           case-fold-search old-case-fold-search)
  4053.     (if (or (and search line-okay)
  4054.         (null search))
  4055.         (progn
  4056.           (setq done t)
  4057.           (if (null search)
  4058.           (progn
  4059.             (open-line 1)
  4060.             (unless found (newline))
  4061.             (insert (concat comment-start " " ispell-words-keyword))
  4062.             (if (> (length comment-end) 0)
  4063.             (save-excursion
  4064.               (newline)
  4065.               (insert comment-end)))))
  4066.           (insert (concat " " word))))))))
  4067.  
  4068. (add-to-list 'debug-ignored-errors "^No word found to check!$")
  4069.  
  4070. (provide 'ispell)
  4071.  
  4072.  
  4073. ;;; LOCAL VARIABLES AND BUFFER-LOCAL VALUE EXAMPLES.
  4074.  
  4075. ;; Local Variable options:
  4076. ;; mode: name(-mode)
  4077. ;; eval: expression
  4078. ;; local-variable: value
  4079.  
  4080. ;; The following sets the buffer local dictionary to `american' English
  4081. ;; and spell checks only comments.
  4082.  
  4083. ;; Local Variables:
  4084. ;; mode: emacs-lisp
  4085. ;; comment-column: 40
  4086. ;; ispell-check-comments: exclusive
  4087. ;; ispell-local-dictionary: "american"
  4088. ;; End:
  4089.  
  4090.  
  4091. ;;; MORE EXAMPLES OF ISPELL BUFFER-LOCAL VALUES
  4092.  
  4093. ;; The following places this file in nroff parsing and extended char modes.
  4094. ;; Local IspellParsing: nroff-mode ~nroff
  4095. ;; Change IspellPersDict to IspellPersDict: to enable the following line.
  4096. ;; Local IspellPersDict ~/.ispell_lisp
  4097. ;; The following were automatically generated by ispell using the 'A' command:
  4098. ; LocalWords:  settable alist inews mh frag pdict Wildcards iconify arg tex kss
  4099. ; LocalWords:  alists minibuffer bufferp autoload loaddefs aff Dansk KOI SPC op
  4100. ; LocalWords:  Francais Nederlands charset autoloaded popup nonmenu regexp num
  4101. ; LocalWords:  AMStex hspace includeonly nocite epsfig displaymath eqnarray reg
  4102. ; LocalWords:  minipage modeline pers dict unhighlight buf grep sync prev inc
  4103. ; LocalWords:  fn hilight oldot NB AIX msg init read's bufs pt cmd Quinlan eg
  4104. ; LocalWords:  uuencoded unidiff sc nn VM SGML eval IspellPersDict unsplitable
  4105. ; LocalWords:  lns XEmacs HTML casechars Multibyte
  4106.  
  4107. ;; arch-tag: 4941b9f9-3b7c-4a76-a4ed-5fa8b6010ef5
  4108. ;;; ispell.el ends here
  4109.